Qt学习之路之鼠标事件

本文转载自:CSDN - 知其所以然

原文链接:http://blog.csdn.net/HK_5788/article/details/42529835


鼠标事件包括移动,鼠标按下,释放(松开),单击,双击.......

自定义鼠标事件,需重写虚函数:

  1. void QWidget::mousePressEvent(QMouseEvent * event) [virtual protected]  
  2. void QWidget::mouseReleaseEvent(QMouseEvent * event) [virtual protected]  
  3. void QWidget::mouseMoveEvent(QMouseEvent * event) [virtual protected]  
  4. void QWidget::mouseDoubleClickEvent(QMouseEvent * event) [virtual protected]  
简单示例:

mouse_widget.h

  1. #ifndef MOUSEWIDGET_H  
  2. #define  MOUSEWIDGET_H  
  3.   
  4. #include <QWidget>  
  5. #include <QMainWindow>  
  6. #include <QLabel>  
  7. #include <QStatusBar>  
  8. #include <QMouseEvent>  
  9. #include <QTextCodec>  
  10.   
  11. class MouseEvent : public QMainWindow  
  12. {  
  13.     Q_OBJECT  
  14. public:  
  15.     MouseEvent(QWidget *parent = 0);  
  16.   
  17. protected:  
  18.     void mousePressEvent(QMouseEvent *e);       //--鼠标按下事件  
  19.     void mouseMoveEvent(QMouseEvent *e);    //--鼠标移动事件  
  20.     void mouseReleaseEvent(QMouseEvent *e); //--鼠标释放(松开)事件  
  21.     void mouseDoubleClickEvent(QMouseEvent *e); //--鼠标双击事件  
  22.   
  23. private:  
  24.     QLabel *statusLabel;                //---显示鼠标移动时的实时位置   
  25.     QLabel *mousePointLabel;        //---显示鼠标位置  
  26. };  
  27.   
  28. #endif  //MOUSEWIDGET_H  

mouse_widget.cpp
  1. #include "mouse_widget.h"  
  2.   
  3. MouseEvent::MouseEvent(QWidget *parent /*= 0*/) : QMainWindow(parent)  
  4. {  
  5.     //----Qt5解决中文乱码  
  6.     QTextCodec *codec = QTextCodec::codecForName("GB18030");  
  7.       
  8.     //---显示鼠标移动时的实时位置   
  9.     statusLabel = new QLabel();  
  10.     statusLabel->setText(codec->toUnicode("当前位置:"));  
  11.     statusLabel->setFixedWidth(100);  
  12.   
  13.     //---显示鼠标位置  
  14.     mousePointLabel = new QLabel();  
  15.     mousePointLabel->setText("");  
  16.     mousePointLabel->setFixedWidth(100);  
  17.       
  18.     //---在状态栏增加控件  
  19.     statusBar()->addPermanentWidget(statusLabel);  
  20.     statusBar()->addPermanentWidget(mousePointLabel);  
  21.   
  22.     //---设置当前窗体对鼠标追踪,默认为false,false表示不追踪  
  23.     setMouseTracking(true);  
  24.   
  25.     //----设置窗口属性  
  26.     setWindowTitle(codec->toUnicode("鼠标事件信息"));  
  27.     //----窗口大小  
  28.     resize(400, 150);  
  29. }  
  30.   
  31. //--鼠标按下事件  
  32. void MouseEvent::mousePressEvent(QMouseEvent *e)  
  33. {  
  34.     //----Qt5解决中文乱码  
  35.     QTextCodec *codec = QTextCodec::codecForName("GB18030");  
  36.   
  37.     //----QMouseEvent类提供的x()和y()可获取鼠标相对窗口的位置  
  38.     QString str = "("+QString :: number(e->x()) +", "+QString::number(e->y())+")";  
  39.   
  40.     //---点击左键  
  41.     if (Qt ::LeftButton == e->button())  
  42.     {  
  43.         statusBar()->showMessage(codec->toUnicode("左键:") + str);  
  44.     }  
  45.     //---点击左键  
  46.     if (Qt ::RightButton == e->button())  
  47.     {  
  48.         statusBar()->showMessage(codec->toUnicode("右键:") + str);  
  49.     }  
  50.     //---点击左键  
  51.     if (Qt ::MidButton == e->button())  
  52.     {  
  53.         statusBar()->showMessage(codec->toUnicode("中键:") + str);  
  54.     }  
  55. }  
  56.   
  57. //---鼠标释放(松开)事件  
  58. void MouseEvent::mouseReleaseEvent(QMouseEvent *e)  
  59. {  
  60.     //----Qt5解决中文乱码  
  61.     QTextCodec *codec = QTextCodec::codecForName("GB18030");  
  62.   
  63.     //----QMouseEvent类提供的x()和y()可获取鼠标相对窗口的位置  
  64.     QString str = "("+QString :: number(e->x()) +", "+QString::number(e->y())+")";  
  65.     statusBar()->showMessage(codec->toUnicode("鼠标位置:") + str, 3000);  
  66. }  
  67.   
  68. //--s鼠标双击事件  
  69. void MouseEvent::mouseDoubleClickEvent(QMouseEvent *e)  
  70. {  
  71.     //---没有实现功能  
  72. }  
  73.   
  74. //--鼠标移动事件  
  75. void MouseEvent::mouseMoveEvent(QMouseEvent *e)  
  76. {  
  77.     mousePointLabel->setText("("+QString::number(e->x())+", "+QString :: number(e->y())+")");  
  78. }  
main.cpp
  1. #pragma once  
  2. #include <QtWidgets/QApplication>  
  3. #include "mouse_widget.h"  
  4.   
  5. int main(int argc, char *argv[])  
  6. {  
  7.     QApplication a(argc, argv);  
  8.   
  9.     MouseEvent win;  
  10.     win.show();  
  11.   
  12.     return a.exec();  
  13. }  
运行结果图:



当用户用鼠标在指定区域内触发相应的鼠标事件,窗口的状态栏将会实时显示鼠标的动态信息。如点击鼠标左键,移动鼠标将会在状态栏实时显示鼠标的坐标~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值