2. QT creator Write multi-window programs

Source: Internet
Author: User
Tags qt designer

Functions:

A dialog box appears at the beginning of the program. After you press the button, you can enter the main window. If you close the dialog box directly, you cannot enter the main window, and the entire program will exit. After entering the main window, we press the button to bring up a dialog box. No matter how you close this dialog box, it will return to the main window.

Implementation principle:

In the program, we first create a main project as the main interface, then create a dialog box class, add it to the project, and then call the self-created dialog box class in the program to implement multiple windows.

Implementation process:

1. Create a qt4 GUI application project named ngui and select qwidget as the base class. After the project files are created, the list is shown in.

2. Create a dialog box class. For example, in the new building, select QT designer form class.

3. Select dialog without buttons.

4. Set the class name to mydlg.

5. Click Finish. Note that it has been added to the project we just created by default.

6. for example, in mydlg. drag a push button into the UI, change the text on it to "Enter the main window", and change its objectname to enterbtn in Its Properties window, in the following signals and slots Editor, associate the signal with the slot. Set sender to enterbtn, set signal to clicked (), and receive to mydlg, the slot is set to accept (). In this way, you can click this button to close the dialog box and send an accepted signal. We will use this signal below.

7. Modify the main function main. cpp as follows:

# Include <qtgui/qapplication>
# Include "widget. H"
# Include "mydlg. h" // Add the header file
Int main (INT argc, char * argv [])
{
Qapplication A (argc, argv );
Widget W;
Mydlg my1; // create the object my1 of the newly created class
If(my1.exe C () = qdialog: accepted) // use the accepted signal to determine whether enterbtn is pressed
{
W. Show (); // if pressed, the main window is displayed
Return a.exe C (); // The program is executed until the main window is closed.
}
Else return 0; // if it is not pressed, it will not enter the main window and the entire program ends.
}

The main function must be written in this way to complete the required functions.

If the main function is written as follows:

# Include <qtgui/qapplication>
# Include "widget. H"
# Include "mydlg. h"
Int main (INT argc, char * argv [])
{
Qapplication A (argc, argv );
Mydlg my1;
If(my1.exe C () = qdialog: accepted)
{

Widget W;

W. Show ();
}
Return a.exe C ();

 

}

In this way, because W is defined in the IF statement, it becomes invalid after the if statement is executed. The consequence is that after you press enterbtn, the window on the main interface will disappear. If the program is changed at this time, click "run" again,Error: collect2: LD returned 1 exit status. This is because the main window is not displayed, but it is hidden and the program is not finished, but runs in the background. Therefore, an error occurs when you change the program. You can press the red stop STOP button on the debugging bar to stop the program running. You can also end the process in the Process of Windows Task Manager, and then run it again. Of course, you should first disable QT creator and then re-open it, which can solve the problem.

If you change the program to the following:

# Include <qtgui/qapplication>
# Include "widget. H"
# Include "mydlg. h"
Int main (INT argc, char * argv [])
{
Qapplication A (argc, argv );
Mydlg my1;

Widget W;

If(my1.exe C () = qdialog: accepted)
{
W. Show ();
}
Return a.exe C ();

}

 

In this way, although the problem of the above Main Window flashing is solved, if you close the dialog box directly without entering enterbtn when the my1 dialog box appears, then the entire program should be executed, but is that true? If you change the program at this time and press the run button again, you will find thatError: collect2: LD returned 1 exit statusThis indicates that the program is not over. We can open the Windows Task Manager and we can see that our program is still running.

BecauseReturn a.exe C ();If the main window does not exit, the program will be executed all the time. Therefore, only the first method is used to place the statement into the if statement, while the else statementElse return 0 ;,In this way, if enterbtn is not pressed, the execution will end.

At this point, we have implemented an interface to end the execution, and then the program of another interface is displayed. Next we will add a button in the main window and press this button to bring up a dialog box, but this dialog box is closed, so that the main window will not be closed.

8. For example, if you add a button to the main window, the text is displayed as a "pop-up dialog box". Right-click it and select go to slot from the pop-up menu.

9. Click Event clicked ().

10. In the pop-up slot function, add the following sentence:

My2.show ();

My2 is another object of the new dialog box class, but my2 is not defined yet, so in the widget. add the corresponding code to the H file, as shown below. First add the header file and then add the Definition Statement of my2. Here we put it in private, because the general functions are put in public, all variables are placed in private.

# Ifndef widget_h

# Define widget_h

# Include <qtgui/qwidget>

# Include "mydlg. h" // contains the header file

Namespace UI

{

Class widget;

}

Class Widget: Public qwidget

{

Q_object

Public:

Widget (qwidget * parent = 0 );

~ Widget ();

PRIVATE:

Ui: widget * UI;

Mydlg my2; // define my2

Private slots:

Void on_pushbutton_clicked ();

};

# Endif // widget_h

Now, run the program to complete the functions required by our experiment. In the whole program, we use two methods to associate the signal with the slot function. The first button is directly implemented in the designer; we wrote the slot function statement for the second button. In fact, the graphic design is the same as that for writing code directly.

In this program, we implement two types of window opening methods. One is to disappear and then open another window, and the other is to open another window without disappearing itself. We can see that their implementation methods are different.

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.