QT从入门到实战完整版 (第一次作业)

创建三个按钮,第一个打开一个新的页面,第二个关闭这个新的页面,第三个共有前两个按钮的功能。
#include "widget.h"
#include "ui_widget.h"
#include <QWidget>
#include <QPushButton>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QWidget *one=new QWidget;
    //brt纯打开按钮,brt1纯关闭按钮,brt2变化按钮
    QPushButton *brt=new QPushButton;
    QPushButton *brt1=new QPushButton;
    QPushButton *brt2=new QPushButton;
    brt->setParent(this);
    brt1->setParent(this);
    brt2->setParent(this);
    resize(500,500);
    brt->move(100,200);
    brt1->move(200,200);
    brt2->move(300,200);

    brt->setParent(this);
    brt->setText("打开");
    brt1->setParent(this);
    brt1->setText("关闭");
    brt2->setParent(this);
    brt2->setText("打开/关闭");

    setWindowTitle("主页面");
    connect(brt,&QPushButton::clicked,one,[=](){
        one->show();
    });
    connect(brt1,&QPushButton::clicked,one,[=](){
        one->close();
    });
    connect(brt2,&QPushButton::clicked,one,[=](){
        if(brt2->text()=="close")
        {
            one->close();
            brt2->setText("open");
        }
        else{
            one->show();
            brt2->setText("close");
        }
    });
}
Widget::~Widget()
{
    delete ui;
}
posted @ 2023-07-12 18:39  Vijurria  阅读(16)  评论(0编辑  收藏  举报