dialog Box Dialog

Source: Internet
Author: User

Qmainwindow

QMainWindowis a pre-defined main window class that is brought by the QT framework.

The main window is the top-most window of an application (not a game or the like) in a normal sense. It is usually made up of a title bar, a menu bar, several toolbars, and a task bar. Between these subcomponents is our workspace.

Adding actions to add menus and toolbars, such as adding an open menu and tools

1Qaction *openaction;2Openaction =NewQaction (Qicon (":/img/open"), TR ("&open"), This);3Openaction->setshortcut (Qkeysequence::open);//assign a shortcut key to a menu4Openaction->setstatustip (TR ("Open an existing file"));//Tips5 6  //Join the menu bar in file7Qmenu *file = MenuBar ()->addmenu (TR ("&file"));8File->addaction (openaction);9     Ten  //Add Toolbar OneQtoolbar *toolbar = Addtoolbar (tr ("&file")); AToolbar->addaction (openaction);

Qdialog

Using the QDialog Class Implementation dialog box in Qt

QDialog(and its subclasses, and Qt::Dialog classes of all types) have an additional explanation for its parent pointer: If the parent is NULL, the dialog box acts as a top-level window, otherwise it is the parent component's Child dialog box (at which point the default appears at the center of the parent). The difference between a top-level window and a non-top-level window is that the top-level window has its own position in the taskbar, and not the top-level window that shares its parent component's location.

1 qdialog Dialog;    // top level, displayed in taskbar 2 Qdialog Dialog (this)    // child window, not shown in taskbar

Modal and non-modal

dialog boxes are divided into modal dialogs and non-modal dialog boxes. A modal dialog box blocks the input of other windows in the same application. Modal dialogs are common, such as the "Open Files" feature. You can try to open the file in Notepad, and when the Open File dialog box appears, we cannot manipulate the window part except this dialog box. In contrast to non-modal dialogs, such as the Find dialog, we can continue to edit the contents of Notepad while the Find dialog box is displayed.

Qt supports modal dialog boxes and non-modal dialogs. Qt has two levels of modal dialogs: application-level modal and window-level modal, which by default is application-level mode. Application-level modal means that when the modal dialog box appears, the user must first interact with the dialog box until the dialog box is closed before accessing other windows in the program. Modal at the window level means that the modal only blocks the window associated with the dialog box, but still allows the user to interact with other windows in the program.

Qt uses a modal dialog box that implements QDialog::exec() the application level, using a modal dialog box that implements the QDialog::open() window level, using the QDialog::show() Implement non-modal dialog box.

Modal:

1 void Mainwindow::open () 2 {3    qdialog Dialog; 4     Dialog.setwindowtitle (tr ("Hello")); 5     dialog.exec (); 6 }

Non-modal: note that it must be created with new or flash over, because the show() function does not block the current thread, the dialog box is displayed, and the function returns immediately, and the code continues to execute. The new is built on the heap and will not disappear after execution.

However, there is a problem with this: What if our dialog box does not appear in an interface class? Because the parent of Qwidget must be a qwidget pointer, it restricts the inability to pass a normal C + + class pointer to the Qt dialog box. In addition, if the memory consumption is strictly limited, when we use the main window as the parent, the main window does not close, the dialog box will not be destroyed, so it will always occupy memory. In this scenario, we can set the dialog Windowattribute :

1 void Mainwindow::open () 2 {3     New Qdialog; 4     Dialog->SetAttribute (qt::wa_deleteonclose); 5     Dialog->setwindowtitle (tr ("Hello, dialog! " )); 6     Dialog->Show (); 7 }

dialog Box Dialog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.