Qt::WA_TransparentForMouseEvents用法
Qt助手中對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.
大意是:
當(dāng)該屬性被激活啟用時(shí),將會(huì)使所有發(fā)送到窗體和窗體內(nèi)部子控件的鼠標(biāo)事件無效。鼠標(biāo)事件被分發(fā)到其它的窗體部件,就像本窗體部件及本窗體內(nèi)的子控件沒有出現(xiàn)在窗體層次體系中。鼠標(biāo)單擊和鼠標(biāo)其它事件高效地穿過(即繞開)本窗體部件及其內(nèi)的子控件,這個(gè)屬性默認(rèn)是禁用未開啟的。
測試?yán)尤缦?#xff1a;
#include "QtWidgetsApplication1.h" #include "QtTestWidget.h" #include<QDebug>QtWidgetsApplication1::QtWidgetsApplication1(QWidget* parent): QWidget(parent) {ui.setupUi(this);setWindowTitle("parent");QtTestWidget* p = new QtTestWidget(this);QVBoxLayout* pLayout = new QVBoxLayout(this);pLayout->addWidget(p);pLayout->addWidget(ui.pushButton);setLayout(pLayout);// 設(shè)置本窗體上的類型為QtTestWidget的子窗體p的// Qt::WA_TransparentForMouseEvents屬性為true,則// 在p上單擊鼠標(biāo)左鍵,不能響應(yīng)QtTestWidget的mousePressEvent函數(shù)。p->setAttribute(Qt::WA_TransparentForMouseEvents, true);// 本窗體將Qt::WA_TransparentForMouseEvents設(shè)置為true,在本窗體單擊鼠標(biāo)左鍵// 依然能進(jìn)入本窗體的mousePressEvent函數(shù)。setAttribute(Qt::WA_TransparentForMouseEvents, true);// 設(shè)置本窗體上的一個(gè)按鈕的Qt::WA_TransparentForMouseEvents為true,則// 單擊該按鈕,不能進(jìn)入按鈕的單擊信號(hào)響應(yīng)槽函數(shù)btnClicked。ui.pushButton->setAttribute(Qt::WA_TransparentForMouseEvents, true);connect(ui.pushButton, &QPushButton::clicked, this, &QtWidgetsApplication1::btnClicked); }QtWidgetsApplication1::~QtWidgetsApplication1() {}// 當(dāng)按鈕的Qt::WA_TransparentForMouseEvents為true,單擊按鈕,該函數(shù)不能響應(yīng) void QtWidgetsApplication1::btnClicked() {qDebug() << "btnClicked" << "\r\n"; }// 即使設(shè)置了本窗體的Qt::WA_TransparentForMouseEvents為true,該函數(shù)依然能進(jìn)入 void QtWidgetsApplication1::mousePressEvent(QMouseEvent* event) {qDebug() << "mousePressEvent" << "\r\n"; }QtTestWidget.cpp如下:
#include "QtTestWidget.h" #include<QDebug>QtTestWidget::QtTestWidget(QWidget *parent): QWidget(parent) {ui.setupUi(this);}QtTestWidget::~QtTestWidget() { }void QtTestWidget::btnClicked() {}void QtTestWidget::mousePressEvent(QMouseEvent* event) {qDebug() << "5555" << "\r\n";QWidget::mousePressEvent(event); }在QtWidgetsApplication1的非pushButton占據(jù)區(qū)域即子窗體QtTestWidget占據(jù)的區(qū)域上單擊鼠標(biāo)右鍵,結(jié)果如下:
mousePressEvent mousePressEvent可見QtTestWidget::mousePressEvent沒響應(yīng),單擊pushButton,輸出結(jié)果依然是上面的,可見pushButton槽函數(shù)沒響應(yīng)。QtWidgetsApplication1窗體即本窗體設(shè)置了setAttribute(Qt::WA_TransparentForMouseEvents, true), 但本窗體的mousePressEvent依然響應(yīng)了,可見Qt::WA_TransparentForMouseEvents只對本窗體內(nèi)的子窗體有效,對本窗體無效,跟Qt助手說的還是有些不同。
Qt::WA_TransparentForMouseEvents的一個(gè)應(yīng)用場景如下:
有個(gè)需求:在業(yè)務(wù)開始時(shí),線程自動(dòng)向每個(gè)按鈕發(fā)送clicked()信號(hào),按鈕接收到該信號(hào)后,執(zhí)行該信號(hào),從而模擬人單擊按鈕,但整個(gè)業(yè)務(wù)過程中,按鈕不能接收鍵盤鼠標(biāo)事件,防止人干擾線程自動(dòng)模擬按鈕的執(zhí)行。首先想到的是調(diào)用按鈕的setEnable(false)函數(shù),這樣確實(shí)可以使按鈕不接收鍵盤鼠標(biāo)消息,但按鈕也不響應(yīng) clicked()信號(hào)了,也就是達(dá)不到用線程模擬人工手動(dòng)按按鈕的功能。
正確的做法是利用如下代碼:
? ??
pBtn->setAttribute(Qt::WA_TransparentForMouseEvents, true);即將按鈕的Qt::WA_TransparentForMouseEvents設(shè)置為true。
總結(jié)
以上是生活随笔為你收集整理的Qt::WA_TransparentForMouseEvents用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 没发新车令股价下挫,外媒:特斯拉Mode
- 下一篇: SVN trunk(主线) branch