Qt show modal dialog (.ui) on menu item click

后端 未结 2 1948
挽巷
挽巷 2021-02-02 10:07

I want to make a simple \'About\' modal dialog, called from Help->About application menu. I\'ve created a modal dialog window with QT Creator (.ui file).

What code shou

2条回答
  •  忘了有多久
    2021-02-02 10:54

    For modal dialogs, you should use exec() method of QDialogs.

    about = new QDialog(0, 0);
    
    // The method does not return until user closes it.
    about->exec();
    
    // In this point, the dialog is closed.
    

    Docs say:

    The most common way to display a modal dialog is to call its exec() function. When the user closes the dialog, exec() will provide a useful return value.


    Alternative way: You don't need a modal dialog. Let the dialog show modeless and connect its accepted() and rejected() signals to appropriate slots. Then you can put all your code in the accept slot instead of putting them right after show(). So, using this way, you wouldn't actually need a modal dialog.

提交回复
热议问题