我對 Qt 有疑問,Windows 上的行為與 Mac 或 Linux 不同。我在 PySide2 上發現了這個問題,但也能夠在最小的 C 應用程式中重現它(見下文)。
當我通過單擊外部關閉彈出視窗小部件時,應用程式的其余部分將忽略單擊。這是所需的行為,也是它在 Linux 和 Mac 上的作業方式。但是,在 Windows 中,此單擊是由單擊的小部件注冊的,在我的應用程式中,這會導致底層小部件上出現不需要的用戶輸入。
有沒有辦法防止在 Windows 中傳遞解雇點擊?我對這個問題的平臺相關代碼很好。
可以使用此示例重現該行為。當彈出視窗打開并testButton單擊時,onTestButton將執行該方法。
#include "mainwindow.h"
#include <QPushButton>
#include <QHBoxLayout>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->setFixedHeight(600);
this->setFixedWidth(800);
QDialog* w = new QWidget();
QWidget* popUp = new QDialog();
popUp->setFixedHeight(200);
popUp->setFixedWidth(200);
popUp->setWindowFlag(Qt::Popup | Qt::FramelessWindowHint);
popUp->setModal(true);
popUp->setVisible(false);
QHBoxLayout *layout = new QHBoxLayout(this);
w->setLayout(layout);
QPushButton* openButton = new QPushButton("open popup");
// same behavior whether using QDialog::show or QDialog::exec
connect(openButton, &QPushButton::clicked, popUp, &QDialog::show);
QPushButton* testButton = new QPushButton("catch mouse");
connect(testButton, &QPushButton::clicked, this, &MainWindow::onTestButton);
layout->addWidget(openButton);
layout->addWidget(testButton);
this->layout()->addWidget(w);
}
void MainWindow::onTestButton()
{
qDebug() << "caught mouse";
}
uj5u.com熱心網友回復:
QWidget 只是一個小部件,如果您希望它“保持”焦點,請改用 QDialog。如果你將它用作 QDialog.show 或 QDialog.exec,它也會改變“邏輯”。您可以嘗試使用 QDialog 嗎?
uj5u.com熱心網友回復:
QDialog::setModal(bool) 應將對話框設定為模態,從而禁止單擊主視窗。
您的 windowflagQt::Popup阻止了模態。如果您想使用該方法并創建Qwidget模式對話框 ( Qt::Dialog),則應從另一個視窗啟動它,或者有一個父視窗并與該QWidget::windowModality屬性一起使用。
測驗:
auto w = new QWidget;
QDialog* popUp = new QDialog;
popUp->setFixedHeight(200);
popUp->setFixedWidth(200);
popUp->setWindowFlags( Qt::FramelessWindowHint);
popUp->setModal(true);
QHBoxLayout *layout = new QHBoxLayout(this);
w->setLayout(layout);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/413479.html
標籤:
