Qt 實現控件不響應鼠標點擊事件


QT中很多控件都有鼠標點擊的事件響應,比如QPushButton,QRadioButton。有時候我們想要實現的是:當鼠標點擊控件時,不會產生響應事件。其中的一種方法是使用Qt::WA_TransparentForMouseEvents。

官方對Qt::WA_TransparentForMouseEvents的說明為

When enabled, this attribute disables the delivery of mouse events to the widget and its children. Mouse events are delivered to other widgets as if the widget and its children were not present in the widget hierarchy; mouse clicks and other events effectively "pass through" them. This attribute is disabled by default.

大概的意思是當使能Qt::WA_TransparentForMouseEvents時,鼠標事件就不會傳送到該控件及子控件,而是會傳到其他的不包含該控件的widget上,這樣的話其實就是實現了鼠標穿透的功能。

實現代碼

ui->radioButton->setAttribute(Qt::WA_TransparentForMouseEvents, true);

使radioButton控件不響應鼠標點擊事件。


免責聲明!

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



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