8. QT-Dialog (modal and non-modal)

Dialog introduction

  • Dialogs are top-level windows for easy user interaction
  • QDialog is the parent class of all dialog windows in Qtand is a container type component
  • QDialog inherits from the QWidget class, as shown in the following figure:

 

 

 

 

What is the difference between QWidget and QDialog

QDialog:

  • QDialog is a special QWidget with customized window styles
  • QDialog can only be used as a dedicated interactive window
  • QDialog cannot be used as a sub-component , embedded in other containers

QWidget:

  • If QWidget has no parent component , it will become a main window,
  • If QWidget has a parent component , it will become a child component of its parent component and embedded in its parent component

 

Code test: difference between QWidget and QDialog

Code 1:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget w;
    w.resize(300,300);

    QDialog d(&w);
    d.resize(150,150);

    w.show();
    d.show();

    return a.exec();
}

Effect:

 

As you can see, the dialog is always used as an independent interactive window

 

Code 2:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget w1;
    w1.resize(300,300);

    QWidget w2(&w1);
    w2.resize(150,150);

    w1.show();
    w2.show();

    return a.exec();
}

Effect:

 

You can see that only one window appears, and the second window does not appear

 

dialog type

modal dialog

  • It is a blocking call , which means that it is impossible to interact with any other window until the dialog box is closed.
  • Used for occasions that depend on user selection, such as: option settings, message prompts, font settings, etc.
  • Creating a modal dialog on the stack is the easiest and most common way
  • Use exec() to enter the message loop of the dialog box to achieve blocking calls

Experimental code:

int main(int argc, char *argv[])
{

    QApplication a(argc, argv);

    QWidget w;
    w.resize(300,300);
w.show(); QDialog d(
&w); d.resize(150,150); d.exec(); // Enter the message loop of dialog box d to realize blocking call return a.exec(); }

 

Modeless dialog  

  • It is a non-blocking call , indicating that when the dialog box appears, it can also interact with the parent window
  • Used for special function settings, such as: search operation
  • In general, non-modal dialogs need to be created on the heap to avoid being automatically destroyed
  • Modeless dialogs need to specify the Qt::WA_DeleteOnClose attribute through the setAttribute() member function
  • Qt::WA_DeleteOnClose means: After exiting the window, let Qt automatically destroy the dialog
  • Use show() to display the dialog window, thus achieving a non-blocking call

Experimental code:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget w;
    w.resize(300,300);
    w.show();

    QDialog  *d = new QDialog(&w);

    d->setAttribute(Qt::WA_DeleteOnClose);
    d->resize(150,150);
    d->show();                

    return a.exec();
}

 

混合模态对话框

  • 在非模态对话框的基础上调用QDialog::setModal(true)成员函数实现

实验代码:

int main(int argc, char *argv[])
{

    QApplication a(argc, argv);

    QWidget w;
    w.resize(300,300);
    w.show();

    QDialog  *d = new QDialog(&w);
    d->setAttribute(Qt::WA_DeleteOnClose);
    d->setModal(true);               
    d->resize(150,150);
    d->show();

    return a.exec();
}

 

对话框返回值处理

  • 只有模态对话框才有返回值概念
  • QDialog::exec()的返回值作为交互结果

  -使用void QDialog::done ( int r ) 作为交互结果,将r值返回给exec()

  -返回值为QDialog::Accepted时,表示用户操作成功

  -返回值为QDialog::Rejected时,表示用户操作失败

  -也可以自定义返回数值

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324730823&siteId=291194637