鼠標事件監控:
對於鼠標時間監控的相關操作,我們在簡歷工程的時候需要選擇的是QWidget基類,不選擇QMainWindow基類,如下所示:
Base class:QWidget
Step1:我們首先定義的是整個Qt軟件界面的UI設計:
根據設計的情況,Qt Creator自動幫我們生成了如下的HTML腳本:

<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Widget</class> <widget class="QWidget" name="Widget"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>Widget</string> </property> <widget class="QPushButton" name="pushButton"> <property name="geometry"> <rect> <x>190</x> <y>230</y> <width>101</width> <height>27</height> </rect> </property> <property name="text"> <string>PushButton</string> </property> </widget> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>-10</x> <y>200</y> <width>171</width> <height>17</height> </rect> </property> <property name="font"> <font> <italic>true</italic> <strikeout>false</strikeout> </font> </property> <property name="text"> <string> PressMove-axisEvent</string> </property> </widget> <widget class="QLabel" name="label_2"> <property name="geometry"> <rect> <x>0</x> <y>256</y> <width>171</width> <height>31</height> </rect> </property> <property name="font"> <font> <italic>true</italic> <strikeout>false</strikeout> </font> </property> <property name="text"> <string> ReleaseMouse-axisEvent</string> </property> </widget> <widget class="QPushButton" name="pushButton_2"> <property name="geometry"> <rect> <x>190</x> <y>200</y> <width>98</width> <height>27</height> </rect> </property> <property name="text"> <string>PushButton</string> </property> </widget> <widget class="QPushButton" name="pushButton_3"> <property name="geometry"> <rect> <x>190</x> <y>260</y> <width>98</width> <height>27</height> </rect> </property> <property name="text"> <string>PushButton</string> </property> </widget> <widget class="QLabel" name="label_3"> <property name="geometry"> <rect> <x>0</x> <y>226</y> <width>171</width> <height>31</height> </rect> </property> <property name="font"> <font> <italic>true</italic> <strikeout>false</strikeout> </font> </property> <property name="text"> <string> PressMouse-axisEvent</string> </property> </widget> <widget class="QLabel" name="label_4"> <property name="geometry"> <rect> <x>0</x> <y>60</y> <width>211</width> <height>51</height> </rect> </property> <property name="font"> <font> <pointsize>22</pointsize> <italic>true</italic> <underline>false</underline> <strikeout>false</strikeout> </font> </property> <property name="cursor"> <cursorShape>BlankCursor</cursorShape> </property> <property name="text"> <string>Mouse Monitor</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> <widget class="QLabel" name="label_5"> <property name="geometry"> <rect> <x>20</x> <y>120</y> <width>171</width> <height>21</height> </rect> </property> <property name="font"> <font> <pointsize>10</pointsize> <italic>true</italic> <underline>false</underline> <strikeout>false</strikeout> </font> </property> <property name="cursor"> <cursorShape>BlankCursor</cursorShape> </property> <property name="text"> <string>Designed by : mm1994uestc</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> <widget class="QLabel" name="label_6"> <property name="geometry"> <rect> <x>0</x> <y>10</y> <width>321</width> <height>51</height> </rect> </property> <property name="font"> <font> <pointsize>28</pointsize> <italic>true</italic> <underline>false</underline> <strikeout>false</strikeout> </font> </property> <property name="cursor"> <cursorShape>BlankCursor</cursorShape> </property> <property name="text"> <string>Qt interface Demo!</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
實際的界面的效果如下所示:
Step2:我們根據設計的實際情況對每一個界面上的Button進行相關的觸發機制:
a)我們需要包含Mouse鼠標相關的頭文件進來
#include <QMouseEvent>
b)鼠標事件的相關函數都封裝在Widget基類當中,針對上面的三類事件Event,我們將列舉如下:
void Widget::mousePressEvent(QMouseEvent *e)
void Widget::mouseMoveEvent(QMouseEvent *e) void Widget::mouseReleaseEvent(QMouseEvent *e)
c)這里我們需要在widget.cpp文件中對每一個事件函數進行實現:(具體實現如下所示)
void Widget::mousePressEvent(QMouseEvent *e)
{
qDebug() << " X , Y"; // 輸出X Y字符串
qDebug() << tr("%1,%2").arg(e->x()).arg(e->y()); // e是我們取得的鼠標對象,我們在對象中取得鼠標對象的相關參數,如x坐標和y坐標
ui->pushButton->setText(tr("(%1,%2)").arg(e->x()).arg(e->y())); // 我們將取得的xy的坐標更新到我們的Button的內容當中
} void Widget::mouseMoveEvent(QMouseEvent *e) { ui->pushButton_2->setText(tr("%1,%2").arg(e->x()).arg(e->y())); // 與上同理 } void Widget::mouseReleaseEvent(QMouseEvent *e) { ui->pushButton_3->setText(tr("%1,%2").arg(e->x()).arg(e->y())); // 與上同理 }
d)我們需要在頭文件widget.h文件當中聲明我們實現的鼠標的相關函數:
protected:
void mousePressEvent(QMouseEvent *); void mouseMoveEvent(QMouseEvent *); void mouseReleaseEvent(QMouseEvent *);
以上就基本完成了整個程序的所有功能,詳細的工程代碼請參考我博客園文件中的內容(文件名稱MouseMonitor.tar.gz):https://i.cnblogs.com/Files.aspx