什么是qt鼠標事件
什么是qt鼠標事件
大家了解qt鼠標事件嗎?不知道的話跟著學(xué)習(xí)啦小編一起來學(xué)習(xí)了解qt鼠標事件吧。
qt鼠標事件大全
1、QMouseEvent中的坐標
QMouseEvent中保存了兩個坐標,一個是全局坐標,當然另外一個是局部坐標。
全局坐標(globalPos())即是桌面屏幕坐標(screen coordinates),這個跟windows下的調(diào)用getCursorPos函數(shù)得到的結(jié)果一致。
局部坐標(pos())即是相對當前active widget的坐標,左上角坐標為(0, 0)。
補充一個公式:
this->mapFromGlobal(this->cursor().pos()) = event.pos()
2、鼠標跟蹤
在qt中,鼠標跟蹤對應(yīng)函數(shù)mouseMoveEvent。但是,默認情況下他并不能如期象你想象的那樣響應(yīng)鼠標的移動。此時,你只需在合適的位置調(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.
默認情況下,mouseMoveEvent響應(yīng)你按下鼠標的某個鍵(拖動,但不局限于左鍵拖動)的鼠標移動。
3、鼠標左鍵拖動和左鍵點擊的判斷
鼠標左鍵點擊很容易判斷,一般就是在重寫mousePressEvent函數(shù),示例如下:
void XXXWidget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
// todo ...
}
}
左鍵拖動的判斷一般放在mouseMoveEvent函數(shù)中,但是你不能向上例一樣來判斷,因為該函數(shù)的event參數(shù)總是返回Qt::NoButton。你可以這樣做:
void XXXWidget::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
// todo ...
}
}
參考教材實例,增加了注釋和新功能,方便大家交流學(xué)習(xí)
mouseevent.cpp
#include "mouseevent.h"
MouseEvent::MouseEvent( ):QMainWindow()
{
setWindowTitle(tr("Get Mouse Event"));
setCursor(Qt::CrossCursor); //設(shè)置鼠標為十字星
labelStatus = new QLabel();
labelStatus->setMinimumSize(500,20);
labelStatus->setFrameShape(QFrame::WinPanel);//設(shè)置標簽形狀
labelStatus->setFrameShadow(QFrame::Sunken); //設(shè)置標簽陰影;
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);//自動跟蹤鼠標(默認情況下,mouseMoveEvent響應(yīng)你按下鼠標的某個鍵的鼠標移動。)
}
MouseEvent::~MouseEvent(){
}
void MouseEvent::mouseMoveEvent ( QMouseEvent * e )//鼠標移動事件響應(yīng)
{
//1.獲取局部鼠標位置
labelMousePos ->setText("("+QString::number(e->x())+","+QString::number(e->y())+")");
//2.獲取全局鼠標位置
QPoint coursePoint;
coursePoint = QCursor::pos();//獲取當前光標的位置
labelMousePos ->setText("("+QString::number(coursePoint.x())+","+QString::number(coursePoint.y())+")");
}
void MouseEvent::mousePressEvent ( QMouseEvent * e )//鼠標單擊事件響應(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);//顯示臨時信息
}
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 )//鼠標雙擊事件響應(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 )//鼠標松開事件響應(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();
}
猜你喜歡: