模态是指当子窗口弹出时,焦点始终被强行集中于子窗口,只要子窗口不退出,焦点就不会被释放,不能再切换到其它主窗口。非模态正好相反。
从线程角度讲,模态子窗口的线程是阻塞的,父、子窗口各有一个线程,当创建子窗口时,父线程就被子线程阻塞掉;非模态线程不是阻塞型的,父、子线程可以并行运行。
在Qt的模态中,主要用到QDialog的exec()方法:
1
2
3
4
5
6
7
8
9
10
|
SonDialog dlg(
this
);
int
res = dlg.exec();
if
(res == QDialog::Accepted)
{
QMessageBox::information(
this
,
"INFORMATION"
,
"You clicked OK button!"
);
}
if
(res == QDialog::Rejected)
{
QMessageBox::information(
this
,
"INFORMATION"
,
"You clicked CANCEL button!"
);
}
|
通过exec()的返回值判断用户触发的是哪个按钮。
非模态,主要用到了QDialog中的show()方法:
1
2
3
|
SonDialog *dlg;
dlg =
new
SonDialog(
this
);
dlg->show();
|