1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 #include <QMouseEvent> 6 #include <QWheelEvent> 7 8 class MainWindow : public QMainWindow 9 { 10 Q_OBJECT 11 12 public: 13 MainWindow(QWidget *parent = 0); 14 ~MainWindow(); 15 16 protected: 17 void mousePressEvent(QMouseEvent *event);//点击 18 //void mouseMoveEvent(QMouseEvent *event);//移动 19 //void mouseReleaseEvent(QMouseEvent *event);//释放 20 //void mouseDoubleClickEvent(QMouseEvent *event);//双击 21 //void wheelEvent(QWheelEvent *event);//滚动 22 23 private: 24 QPoint offset;//存储鼠标位置与窗口位置的差值 25 QCursor cursor;//创建光标,保存光标的默认形状 26 }; 27 28 #endif // MAINWINDOW_H 29 30 //鼠标触发事件
1 #include "mainwindow.h" 2 #include <QMouseEvent> 3 #include <QDebug> 4 5 MainWindow::MainWindow(QWidget *parent) 6 : QMainWindow(parent) 7 { 8 //mousePressEvent(QMouseEvent *event); 9 //mouseDoubleClickEvent(); 10 //mouseMoveEvent(); 11 //mouseReleaseEvent(); 12 //wheelEvent(); 13 } 14 15 MainWindow::~MainWindow() 16 { 17 18 } 19 //左右键点击事件,打印消息 20 void MainWindow::mousePressEvent(QMouseEvent *event) 21 { 22 if(event->buttons()==Qt::LeftButton) 23 { 24 //cursor = new QCursor; 25 qDebug()<<"LeftButton clicked!"; 26 } 27 else if(event->button()==Qt::RightButton) 28 qDebug()<<"RightButton clicked!"; 29 } 30 /*void MainWindow::mouseMoveEvent(QMouseEvent *event) 31 { 32 33 } 34 void MainWindow::mouseDoubleClickEvent(QMouseEvent *event) 35 { 36 if(event->button()==) 37 }*/