Qt dialog bis: modal, non-modal, modal dialog box half

A modal dialog box

Modal Dialog: Dialog blocking the same application window other visual input. Modal dialog boxes have their own event loop, user interaction must complete this dialog, and closed it to access any other window in the application after.

The most common way to display a modal dialog box is calling its exec () function, when the user closes the dialog, exec () will return a useful value, and the case where the flow proceeds from the control calls exec () a. Typically, to get close the dialog and returns the corresponding value, we connect the default button, for example: "OK" button is connected to the accept () slot, "Cancel" button is connected to the Reject () slot. In addition, we can also connect done () slot, it is passed to Accepted or Rejected.


Code Example 1:

MainWindow *pMainWindow = new MainWindow();
pMainWindow->setWindowTitle(QStringLiteral("主界面"));
pMainWindow->show();
 
CustomWindow *pDialog = new CustomWindow(pMainWindow);
pDialog->setWindowTitle(QStringLiteral("模态对话框"));
 
// 关键代码
pDialog->exec();
 
// 关闭模态对话框以后才会执行下面的代码
pMainWindow->setWindowTitle(QStringLiteral("主界面-模式对话框"));
qDebug() << QStringLiteral("关闭模态对话框以后,可以继续向下执行");


Code Example 2:

We can call accept () or Reject () function is such that the exec () function ends the following code:

//可以在之前的代码的快要结束的额时候调用accept();然后在主函数中
login *user_login=new login;//login是继承dialog的类

int res = user_login->exec();
if (res == QDialog::Accepted)
{
    delete user_login;
}


Second, the non-modal dialog box

Modeless dialog boxes: other procedures and the same operations regardless of the window dialog box. Find and Replace dialog text mode is typically non-processing, it allows users to interact with the main application windows and dialog boxes. Call show () to display a non-modal dialog box and immediately returns control to the caller.


Sample code:

MainWindow *pMainWindow = new MainWindow();
pMainWindow->setWindowTitle(QStringLiteral("主界面"));
pMainWindow->show();
 
CustomWindow *pDialog = new CustomWindow(pMainWindow);
pDialog->setWindowTitle(QStringLiteral("非模式对话框"));
 
// 关键代码
pDialog->show();
 
// 下面的代码会立即运行
pMainWindow->setWindowTitle(QStringLiteral("主界面-非模式对话框"));
qDebug() << QStringLiteral("立即运行");
  • The main interface is not blocked, you can do anything click, drag and so on.
  • show the code (after) will be executed immediately.


Third, the half-modal dialog

Call setModal (true) or setWindowModality (), then show (). Unlike exec (), show () returns control to the caller immediately.


MainWindow *pMainWindow = new MainWindow();
pMainWindow->setWindowTitle(QStringLiteral("主界面"));
pMainWindow->show();
 
CustomWindow *pDialog = new CustomWindow(pMainWindow);
pDialog->setWindowTitle(QStringLiteral("半模式对话框"));
 
// 关键代码
pDialog->setModal(true);
pDialog->show();
 
// 下面的代码会立即运行
pMainWindow->setWindowTitle(QStringLiteral("主界面-半模式对话框"));
qDebug() << QStringLiteral("立即运行");
  • The main interface is blocked and can not carry out any operation click, drag and so on.
  • show the code (after) but it will be executed immediately.


reference:

Qt's model, non-mode, semi-modal dialog


Guess you like

Origin www.cnblogs.com/linuxAndMcu/p/11546208.html