Qt5 learning - Click a button to achieve the same open and close a child window

Requirements: After clicking in the main window [Open] button to bring up a child window, and the text on the button changes to [Close] Click to close the child window again!
Analysis: In fact, the sub-window pop-up is not difficult to achieve, the key is to achieve the same button to open and close function, the need to pay attention to ways to achieve as well as some details need to use lambda expressions, follow these steps:
1, first create a new project , create an empty file window, shown in Figure 1, and then right-click on the project, select Add a new file, select the C ++ Class type and select QWidget-based, create a file named childWin child window, as shown in Figure 2.
Here Insert Picture Description
Here Insert Picture Description
2, the code widget.h file to add the following, wherein the selected public class in the main window and the child window is created button objects are globally to be able to use, these objects may be used in the slot function.

#include <QWidget>
#include "childwin.h"
#include <QPushButton>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    //创建按钮对象和子窗口对象
    QPushButton *btn;
    childWin *win;
    //弹出/关闭子窗口的槽函数
    void showChildWin();
    
private:
    Ui::Widget *ui;
};

3, code widget.cpp file below, add their own basically have a comment, I'll pick a few key code speak. First, initialization button and the sub-window object, the button initial text provided [start]; definition function in the slot function, the acquisition button text is [Open] then click pop subwindow [Close] then click to close the window; Finally, connect signals and slots, and to achieve click [start] button text into Close on this point I'm using lambda expressions, you can define these functions directly connect, where this-> showChildWin (); is slot function call, click the button starting signal will call the function, then use btn-> setText ( "closed"); button text will change, so click again closes the child window! Operating results as shown below:

#include "widget.h"
#include "ui_widget.h"
#include<QPushButton>
#pragma execution_character_set("utf-8"); //如果中文出现乱码加上这行!!!

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //定义主窗口大小
    resize(380,300);
    setWindowTitle("主窗口");
    //创建一个子窗口对象
    this->win = new childWin;
    win->resize(350,200);
    win->setWindowTitle("子窗口");
    //创建一个按钮对象
    this->btn = new QPushButton("打开",this);
    btn->move(100,80);
    //连接信号和槽,利用lambda表达式改变按钮上的文字
    connect(btn,&QPushButton::clicked,this,[=](){
        this->showChildWin();
        btn->setText("关闭");
    });
    //connect(btn,&QPushButton::clicked,this,&Widget::showChildWin);
}
//定义弹出子窗口的槽函数
void Widget::showChildWin()
{
    if(btn->text()=="打开")
        win->show();
    if(btn->text()=="关闭")
        win->close();
}

Widget::~Widget()
{
    delete ui;
}

Here Insert Picture Description
Here Insert Picture Description
4. Note: The use lambda expressions () in the connect because you can not btn-> setText ( "closed"); phrase into the slot function in win-> show (); later, because the system is based on sequential read program, you immediately set to [Close] will be executed immediately win-> close () ;, the result is click button to open the child window will automatically close, rather than clicking the button again to close! This point to note! There is not in the slot function to initialize the sub-window, or click on the button again will create a new child window pops up, instead of closing the child window chatter before!

Published 17 original articles · won praise 3 · Views 1799

Guess you like

Origin blog.csdn.net/weixin_43350361/article/details/105146300