I tested on a "windows7", follows the code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Dialog *m1 = new Dialog(this);
Dialog *m2 = new Dialog(this);
Dialog *m3 = new Dialog(this);
m1->setWindowModality(Qt::WindowModal);
m1->setWindowTitle(tr("Window 1"));
m2->setWindowModality(Qt::WindowModal);
m2->setWindowTitle(tr("Window 2"));
m3->setWindowModality(Qt::WindowModal);
m3->setWindowTitle(tr("Window 3"));
m1->show();
m2->show();
m3->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
The only window that can be clicked is the Dialog * m3
, the other is blocked, but when you click MainWindow
you may notice that the effect is different.
If you click the Dialog * m1
or Dialog * m2
to Dialog * m3
window does not BLINK (effect blink),
but if I click MainWindow
to Dialog * m3
window will BLINK (effect blink).
That is the type modal windows do not block because it is waiting for a response,
ie to call it:
Dialog *m1 = new Dialog(this);
Dialog *m2 = new Dialog(this);
Dialog *m3 = new Dialog(this);
I'll have to confirm the response one at a time, because it works in a synchronized and not asynchronized mode, in other words, the type Modal windows are intended to send a request to the parent window, so how much you do not complete the request all cascade windows are blocked. In other words, windows modal (with "parent" setted), will always expect an response (like the "exec").
Alternative solution:
You can create a lock for "MainWindow window" using setEnable (false)
and when all dialogs are closed to "MainWindow window" is enabled with setEnable (true)
. See example:
note: if you will use a modal window to submit responses to the parent window, in this example you need to use the SLOTS: accept()
, reject()
or finished(int)
mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
public slots:
void showChilds();
void findDialogs(const int a = -1);
};
mainwindow.cpp
#include "dialog.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(showChilds()));
}
void MainWindow::showChilds() {
setEnabled(false);
Dialog *m1 = new Dialog(this);
Dialog *m2 = new Dialog(this);
Dialog *m3 = new Dialog(this);
m1->setAttribute(Qt::WA_DeleteOnClose, true);
m2->setAttribute(Qt::WA_DeleteOnClose, true);
m3->setAttribute(Qt::WA_DeleteOnClose, true);
m1->setWindowTitle("1");
m2->setWindowTitle("2");
m3->setWindowTitle("3");
m1->show();
m2->show();
m3->show();
QObject::connect(m1, SIGNAL(finished(int)), this, SLOT(findDialogs(int)));
QObject::connect(m2, SIGNAL(finished(int)), this, SLOT(findDialogs(int)));
QObject::connect(m3, SIGNAL(finished(int)), this, SLOT(findDialogs(int)));
}
void MainWindow::findDialogs(const int a)
{
if(a==-1){
QObject *tmp;
QObjectList list = this->children();
const int j = list.length();
for(int i=0; i < j; i++) {
tmp = (QObject *) (list.at(i));
if(tmp!=0 && tmp->objectName()=="Dialog") {
return;
}
}
setEnabled(true);
} else {
QTimer::singleShot(1, this, SLOT(findDialogs()));
}
}