问题
Is it possible that if a QDialog
instance is exec()
uted, the entire operating system is blocked until the user closes the dialog? In the following minimal example the dialog blocks only its parent widget but not the OS elements outside of the Qt application.
rootwindow.h
#ifndef ROOTWINDOW_H
#define ROOTWINDOW_H
#include <QApplication>
#include <QMainWindow>
#include <QtDebug>
#include <QDialog>
#include <QPushButton>
#include <QMessageBox>
#include <QBoxLayout>
class RootWindow : public QMainWindow
{
Q_OBJECT
private:
QWidget *widgetCentral;
QBoxLayout *layoutMain;
QPushButton *button;
QDialog *dialog;
public:
RootWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0);
~RootWindow();
private slots:
void slotClicked();
};
#endif // ROOTWINDOW_H
rootwindow.cpp
#include "rootwindow.h"
RootWindow::RootWindow(QWidget *parent, Qt::WindowFlags flags): QMainWindow(parent, flags)
{
setCentralWidget( widgetCentral = new QWidget );
widgetCentral->setLayout( layoutMain = new QBoxLayout(QBoxLayout::LeftToRight) );
layoutMain->addWidget(button = new QPushButton("Click me"));
dialog = new QDialog(this);
dialog->setModal(true);
dialog->setWindowModality(Qt::ApplicationModal);
connect(button, &QPushButton::clicked, this, &RootWindow::slotClicked);
}
RootWindow::~RootWindow()
{
}
void RootWindow::slotClicked()
{
int i = dialog->exec();
qDebug() << "Dialog result: " << i;
}
main.cpp
#include "rootwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
RootWindow w;
w.show();
return a.exec();
}
回答1:
Short Answer: You can't. There may be a way using native API, but I doubt it.
However, there is a way you can archive similar behavior: Show a frameless fullscreen window with an opacity of 1% - This window will be invisible to the user, but block all mouse input. Then show a normal dialog on top of that window.
Please note that this approach is merely a workaround and will not work with multiple desktops. In addition to that, some applications (like the task-manager) will still stay on top of your window. Keyboard shortcust like Alt+Tab, The windows-key and others will still work normally. And more...
And last but not least: Even if you could, you shouldn't. Blocking the whole computer is a bad behavior for your application. Showing a normal, application modal dialog should be enough! If the user doesn't want to pay attention to your program, you should not force him!
来源:https://stackoverflow.com/questions/34620247/make-qdialog-modal-to-operating-system