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.
大意是:
當該屬性被激活啟用時,將會使所有發送到窗體和窗體內部子控件的鼠標事件無效。鼠標事件被分發到其它的窗體部件,就像本窗體部件及本窗體內的子控件沒有出現在窗體層次體系中。鼠標單擊和鼠標其它事件高效地穿過(即繞開)本窗體部件及其內的子控件,這個屬性默認是禁用未開啟的。
測試例子如下:
#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);// 設置本窗體上的類型為QtTestWidget的子窗體p的// Qt::WA_TransparentForMouseEvents屬性為true,則// 在p上單擊鼠標左鍵,不能響應QtTestWidget的mousePressEvent函數。p->setAttribute(Qt::WA_TransparentForMouseEvents, true);// 本窗體將Qt::WA_TransparentForMouseEvents設置為true,在本窗體單擊鼠標左鍵// 依然能進入本窗體的mousePressEvent函數。setAttribute(Qt::WA_TransparentForMouseEvents, true);// 設置本窗體上的一個按鈕的Qt::WA_TransparentForMouseEvents為true,則// 單擊該按鈕,不能進入按鈕的單擊信號響應槽函數btnClicked。ui.pushButton->setAttribute(Qt::WA_TransparentForMouseEvents, true);connect(ui.pushButton, &QPushButton::clicked, this, &QtWidgetsApplication1::btnClicked); }QtWidgetsApplication1::~QtWidgetsApplication1() {}// 當按鈕的Qt::WA_TransparentForMouseEvents為true,單擊按鈕,該函數不能響應 void QtWidgetsApplication1::btnClicked() {qDebug() << "btnClicked" << "\r\n"; }// 即使設置了本窗體的Qt::WA_TransparentForMouseEvents為true,該函數依然能進入 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占據區域即子窗體QtTestWidget占據的區域上單擊鼠標右鍵,結果如下:
mousePressEvent mousePressEvent可見QtTestWidget::mousePressEvent沒響應,單擊pushButton,輸出結果依然是上面的,可見pushButton槽函數沒響應。QtWidgetsApplication1窗體即本窗體設置了setAttribute(Qt::WA_TransparentForMouseEvents, true), 但本窗體的mousePressEvent依然響應了,可見Qt::WA_TransparentForMouseEvents只對本窗體內的子窗體有效,對本窗體無效,跟Qt助手說的還是有些不同。
Qt::WA_TransparentForMouseEvents的一個應用場景如下:
有個需求:在業務開始時,線程自動向每個按鈕發送clicked()信號,按鈕接收到該信號后,執行該信號,從而模擬人單擊按鈕,但整個業務過程中,按鈕不能接收鍵盤鼠標事件,防止人干擾線程自動模擬按鈕的執行。首先想到的是調用按鈕的setEnable(false)函數,這樣確實可以使按鈕不接收鍵盤鼠標消息,但按鈕也不響應 clicked()信號了,也就是達不到用線程模擬人工手動按按鈕的功能。
正確的做法是利用如下代碼:
? ??
pBtn->setAttribute(Qt::WA_TransparentForMouseEvents, true);即將按鈕的Qt::WA_TransparentForMouseEvents設置為true。
總結
以上是生活随笔為你收集整理的Qt::WA_TransparentForMouseEvents用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 没发新车令股价下挫,外媒:特斯拉Mode
- 下一篇: 在战略设计层面提出了域、子域、限界上下文