什么是qt鼠標(biāo)事件
什么是qt鼠標(biāo)事件
大家了解qt鼠標(biāo)事件嗎?不知道的話跟著學(xué)習(xí)啦小編一起來學(xué)習(xí)了解qt鼠標(biāo)事件吧。
qt鼠標(biāo)事件大全
1、QMouseEvent中的坐標(biāo)
QMouseEvent中保存了兩個(gè)坐標(biāo),一個(gè)是全局坐標(biāo),當(dāng)然另外一個(gè)是局部坐標(biāo)。
全局坐標(biāo)(globalPos())即是桌面屏幕坐標(biāo)(screen coordinates),這個(gè)跟windows下的調(diào)用getCursorPos函數(shù)得到的結(jié)果一致。
局部坐標(biāo)(pos())即是相對(duì)當(dāng)前active widget的坐標(biāo),左上角坐標(biāo)為(0, 0)。
補(bǔ)充一個(gè)公式:
this->mapFromGlobal(this->cursor().pos()) = event.pos()
2、鼠標(biāo)跟蹤
在qt中,鼠標(biāo)跟蹤對(duì)應(yīng)函數(shù)mouseMoveEvent。但是,默認(rèn)情況下他并不能如期象你想象的那樣響應(yīng)鼠標(biāo)的移動(dòng)。此時(shí),你只需在合適的位置調(diào)用一下函數(shù)setMouseTracking(true)即可。
If mouse tracking is switched off, mouse move events only occur if a mouse button is pressed while the mouse is being moved.
If mouse tracking is switched on, mouse move events occur even if no mouse button is pressed.
默認(rèn)情況下,mouseMoveEvent響應(yīng)你按下鼠標(biāo)的某個(gè)鍵(拖動(dòng),但不局限于左鍵拖動(dòng))的鼠標(biāo)移動(dòng)。
3、鼠標(biāo)左鍵拖動(dòng)和左鍵點(diǎn)擊的判斷
鼠標(biāo)左鍵點(diǎn)擊很容易判斷,一般就是在重寫mousePressEvent函數(shù),示例如下:
void XXXWidget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
// todo ...
}
}
左鍵拖動(dòng)的判斷一般放在mouseMoveEvent函數(shù)中,但是你不能向上例一樣來判斷,因?yàn)樵摵瘮?shù)的event參數(shù)總是返回Qt::NoButton。你可以這樣做:
void XXXWidget::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
// todo ...
}
}
參考教材實(shí)例,增加了注釋和新功能,方便大家交流學(xué)習(xí)
mouseevent.cpp
#include "mouseevent.h"
MouseEvent::MouseEvent( ):QMainWindow()
{
setWindowTitle(tr("Get Mouse Event"));
setCursor(Qt::CrossCursor); //設(shè)置鼠標(biāo)為十字星
labelStatus = new QLabel();
labelStatus->setMinimumSize(500,20);
labelStatus->setFrameShape(QFrame::WinPanel);//設(shè)置標(biāo)簽形狀
labelStatus->setFrameShadow(QFrame::Sunken); //設(shè)置標(biāo)簽陰影;
labelStatus->setText(tr("Mouse Position:"));
labelStatus -> setFixedWidth (100);
labelMousePos = new QLabel();
labelMousePos->setText(tr(""));
labelMousePos -> setFixedWidth (100);
statusBar()->addPermanentWidget(labelStatus);//給狀態(tài)欄添加永久的部件
statusBar()->addPermanentWidget(labelMousePos);
this->setMouseTracking ( true);//自動(dòng)跟蹤鼠標(biāo)(默認(rèn)情況下,mouseMoveEvent響應(yīng)你按下鼠標(biāo)的某個(gè)鍵的鼠標(biāo)移動(dòng)。)
}
MouseEvent::~MouseEvent(){
}
void MouseEvent::mouseMoveEvent ( QMouseEvent * e )//鼠標(biāo)移動(dòng)事件響應(yīng)
{
//1.獲取局部鼠標(biāo)位置
labelMousePos ->setText("("+QString::number(e->x())+","+QString::number(e->y())+")");
//2.獲取全局鼠標(biāo)位置
QPoint coursePoint;
coursePoint = QCursor::pos();//獲取當(dāng)前光標(biāo)的位置
labelMousePos ->setText("("+QString::number(coursePoint.x())+","+QString::number(coursePoint.y())+")");
}
void MouseEvent::mousePressEvent ( QMouseEvent * e )//鼠標(biāo)單擊事件響應(yīng)
{
QString str="("+QString::number(e->x())+","+QString::number(e->y())+")";
if(e->button()==Qt::LeftButton){
statusBar()->showMessage (tr("Mouse Left Button Pressed:")+str);//顯示臨時(shí)信息
}
else if(e->button()==Qt::RightButton){
statusBar()->showMessage (tr("Mouse Right Button Pressed:")+str);
}
else if(e->button()==Qt::MidButton){
statusBar()->showMessage (tr("Mouse Middle Button Pressed:")+str);
}
}
void MouseEvent::mouseDoubleClickEvent( QMouseEvent * e )//鼠標(biāo)雙擊事件響應(yīng)
{ QString str="("+QString::number(e->x())+","+QString::number(e->y())+")";
if(e->button()==Qt::LeftButton){
statusBar()->showMessage (tr("Mouse Left Button Double Clicked:")+str);
}
else if(e->button()==Qt::RightButton){
statusBar()->showMessage (tr("Mouse Right Button Double Clicked:")+str);
}
else if(e->button()==Qt::MidButton){
statusBar()->showMessage (tr("Mouse Middle Button Double Clicked:")+str);
}
}
void MouseEvent::mouseReleaseEvent ( QMouseEvent * e )//鼠標(biāo)松開事件響應(yīng)
{
QString str="("+QString::number(e->x())+","+QString::number(e->y())+")";
statusBar()->showMessage (tr("Mouser Released:")+str,3000);
}
mouseevent.h
#ifndef MOUSEEVENT_H
#define MOUSEEVENT_H
#include <QtGui>
class MouseEvent : public QMainWindow
{ Q_OBJECT
public:MouseEvent();
~MouseEvent();
private:
QLabel *labelStatus;
QLabel *labelMousePos;
protected :
void mouseMoveEvent ( QMouseEvent * e );
void mousePressEvent ( QMouseEvent * e );
void mouseReleaseEvent ( QMouseEvent * e );
void mouseDoubleClickEvent( QMouseEvent * e );
};
#endif
main.cpp
#include<QtGui/QApplication>
#include "mouseevent.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MouseEvent w;w.show();
return app.exec();
}
猜你喜歡: