Qt 跟蹤鼠標事件:setMouseTracking(true)


mouseTracking:bool

Qt Assistant 解釋:

    該屬性用來設置某個控件是否被跟蹤軌跡。

If mouse tracking is disabled (the default), the widget only receives mouse move events when at least one mouse button is pressed while the mouse is being moved.

    如果該屬性設置fasle,這個控件就算正在移動,也僅僅只有鼠標pressed按下之后,才會接受鼠標移動事件。

If mouse tracking is enabled, the widget receives mouse move events even if no buttons are pressed.

    如果該屬性是true,就算該按鈕不被按下,它也會一直接受鼠標移動的事件。

 

    所以想要實現mouseMoveEvent,若是setMouseTrack(true),直接可以得到監聽事件。若是setMouseTrack(false),只有鼠標按下才會有mouseMove監聽事件響應。

 1 ....  2 
 3 test.widget.setMouseTrack(true);  4 
 5 ....  6 
 7 /**  8 
 9 * @brief 鼠標滑動事件 10 
11 */
12 
13 void TestWidget::mouseMoveEvent(QMouseEvent* event) 14 
15 { 16 
17     QPoint m = event->globalPos(); 18 
19     ui.lblMouseEventGlobalPos->setText(QString("(%1,%2)").arg(m.x()).arg(m.y())); 20 
21     QPoint n = QCursor::pos(); 22 
23     ui.lblCursorPos->setText(QString("(%1,%2)").arg(m.x()).arg(m.y())); 24 
25  
26 
27 }  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM