zoukankan      html  css  js  c++  java
  • mouseTracking

    【1】mouseTracking 追踪鼠标的标志位

    作用:保存窗口部件默认是否接收鼠标移动事件。此成员变量在QWidget类中。

    【2】Qt Assistant 解释

    翻译如下:

    这个属性保存部件窗口是否追踪鼠标。

    如果部件窗口不(默认值)追踪鼠标,那么,当鼠标在部件窗口内移动时候,只有鼠标按键在至少一次被按下之后,这个窗口部件才会接收鼠标移动事件。

    如果部件窗口追踪鼠标,那么即使没有鼠标按键被按下,这个部件窗口也会接收鼠标移动事件。

    总之,窗口部件默认不直接接收鼠标移动事件,想要其默认就直接接收鼠标移动事件,需要预置mouseTracking标志为true。

    【3】示例学习

    文件列表:

    文件1:MainWindows.h(QtCreator 默认创建工程文件,未做修改)

     1 #ifndef MAINWINDOW_H
     2 #define MAINWINDOW_H
     3 
     4 #include <QMainWindow>
     5 
     6 namespace Ui {
     7 class MainWindow;
     8 }
     9 
    10 class MainWindow : public QMainWindow
    11 {
    12     Q_OBJECT
    13 
    14 public:
    15     explicit MainWindow(QWidget *parent = 0);
    16     ~MainWindow();
    17 
    18 private:
    19     Ui::MainWindow *ui;
    20 };
    21 
    22 #endif // MAINWINDOW_H

     文件2:MainWindows.cpp(QtCreator 默认创建工程文件,未做修改)

    #include "MainWindow.h"
    #include "ui_MainWindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }

    文件3:自定义MyLabel,头文件MyLabel.h

     1 #ifndef MYLABEL_H
     2 #define MYLABEL_H
     3 
     4 #include <QLabel>
     5 #include <QMouseEvent>
     6 
     7 class MyLabel : public QLabel
     8 {
     9 public:
    10     MyLabel(QWidget* parent = NULL);
    11 
    12 protected:
    13     void mouseMoveEvent(QMouseEvent *event);
    14     void mousePressEvent(QMouseEvent *event);
    15     void mouseReleaseEvent(QMouseEvent *event);
    16 };
    17 
    18 #endif // MYLABEL_H

     文件4:自定义MyLabel,头文件MyLabel.cpp (此类的构造函数会对mouseTracking进行设置。另外,重实现鼠标移动、鼠标按下、鼠标释放事件

    #include "MyLabel.h"
    
    MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
    {
        setMouseTracking(true);
        setStyleSheet ("background-color:rgb(190, 190, 190); color:rgb(0, 0, 0);");
    }
    
    void MyLabel::mouseMoveEvent(QMouseEvent *event)
    {
        setText(QString("<center><h1>Move:(%1, %2)</h1></center>").arg(QString::number(event->x()),
                QString::number(event->y())));
    }
    
    void MyLabel::mousePressEvent(QMouseEvent *event)
    {
        setText(QString("<center><h1>Press:(%1, %2)</h1></center>").arg(QString::number(event->x()),
                                                                        QString::number(event->y())));
    }
    
    void MyLabel::mouseReleaseEvent(QMouseEvent *event)
    {
        QString msg;
        msg.sprintf("<center><h1>Release:(%d, %d)</h1></center>", event->x(), event->y());
        this->setText(msg);
    }

    文件5: main.cpp

     1 #include "MainWindow.h"
     2 #include <QApplication>
     3 
     4 int main(int argc, char *argv[])
     5 {
     6     QApplication a(argc, argv);
     7     MainWindow w;
     8     w.show();
     9 
    10     return a.exec();
    11 }

     文件6:UI界面设计如下图。注意:一个MainWindows窗体,中央位置拖一个QLable,然后把QLabel提升为MyLabel即可。

    所有文件如上。

    【3】运行效果图

    在MyLabel.cpp文件中,构造函数会对mouseTracking进行设置。可以试着屏蔽设置(默认值为false)再观察窗口接收鼠标事件的效果。

  • 相关阅读:
    深入理解ThreadLocal
    synchronized与Lock的区别与使用
    1亿个数中找出最小的100个数--最小堆
    B+/-Tree原理(mysql索引数据结构)
    深入理解token
    shiro(java安全框架)
    第一次项目上Linux服务器(四:CentOS6下Mysql数据库的安装与配置(转))
    第一次项目上Linux服务器(三:安装Tomcat及相关命令)
    第一次项目上Linux服务器(二:——安装jdk)
    第一次项目上Linux服务器(一:远程连接服务器)
  • 原文地址:https://www.cnblogs.com/Braveliu/p/7162705.html
Copyright © 2011-2022 走看看