QT (B): Simple multi-window program

Original Address: https://houkaifa.com/2019/02/19/LearningNotes-QT-02/

Outline

Benpian records how QT Creator development of a simple multi-window program.
Environment: Win10 + QT Creator4.4.1 + QT5.9.2.
Implementing content: Enter password specified in the main window, click on the button to load another window and the child window is given only once while loading three methods.

Here Insert Picture Description


First, the establishment QT project

Open the QT Creator, the new Qt Widget Application project.

Here Insert Picture Description

The item name, the next step to complete the project has been created.

Here Insert Picture Description

Below is the initial engineering structures:

Here Insert Picture Description

Second, add a new GUI file

Select the Right Forms in the project directory, add new files.

Here Insert Picture Description

Select New File window like Qt, Qt Designer interface class, OK.

Here Insert Picture Description

According to the actual need to select the interface template, select here is still the Main Window, the next step.

Here Insert Picture Description

The default categories or MainWindow, in order to avoid conflict with the main window, re-select the name of the class, all the way to the next step to complete.

Here Insert Picture Description

The new Qt Designer interface to create a class structure after the completion of the project:

Here Insert Picture Description

Third, to achieve loaded window

Open the file .ui the main window, and add a lineEdit a pushButton do a simple interface mean look, which lineEdit set the display to Password, and set the default text:

Here Insert Picture Description

The effect of running up like this:

Here Insert Picture Description

Next we want to achieve is, when the contents of the text box to specify the content, such as when Houkaifa Click on the OK button to load the window ChildWindow.

First, we add the sub-window in the main window .h file header contains:

#include <childwindow.h>

Private members and a child window class in the main window class:

private:
    ChildWindow *cw;

The complete code looks like this:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <childwindow.h>
#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
private:
    ChildWindow *cw;
};

#endif // MAINWINDOW_H

emmm, or put the picture looks clearer:

Here Insert Picture Description

Here, the main window pushButton we added clicked slot function signal, for judging whether the password is correct.

Here Insert Picture Description

Under the password is correct, how to load a child window cw?
In the main window class we define a private member ChildWindow class, just in place to instantiate and call show () function can be displayed, as shown in the specific code:

Here Insert Picture Description

Of course, not the child window class definition, directly created as class members in time of need temporary variables are possible, such as members of cw delete statement, and in the comments on the map (// Load ChildWindow) directly at the following code:

ChildWindow *cw = new ChildWindow(this);
cw->show(); 

F5 debugger, test results:

  • Wrong password is entered, click the OK button, a password prompt error.

Here Insert Picture Description

  • Enter the correct password and click OK button to load ChildWindow.

Here Insert Picture Description

--- END (fake) ---

At this point you will find multiple clicks the OK button loads multiple ChildWindow, if you do not want the child several times while the window is loaded, you can:

  • Method One: In the child window has finished loading after the main window is hidden:
...
cw->show();	// 显示子窗口
this->hide();	// 隐藏主窗口
...
  • Method 2: ChildWindow converted into singleton class.

  • Method three: class member as in the present embodiment as defined in the main ChildWindow window and the child window is closed after the pointer in the empty state when verified pointer click the confirmation button to know whether the window has been created, detailed below:

How to turn off after the pointer in the child window blank?
A: Rewrite closeEvent event function.

closeEvent is QCloseEvent class virtual function, which is a prototype:

void closeEvent(QCloseEvent* event);

Triggered when the window is closed, the class header file included in the child window, and closeEvent rewrite function can be achieved when custom actions child window is closed.

Here Insert Picture Description

Next, for the realization of the function closeEvent .cpp file sub-window, the direct transmission signal:

void ChildWindow::closeEvent(QCloseEvent* event)
{
    emit windowClosed();
}

Finally, the full complement of code to the main window:

Here Insert Picture Description

effect:

Here Insert Picture Description

Published 26 original articles · won praise 24 · views 30000 +

Guess you like

Origin blog.csdn.net/iSunwish/article/details/87858940