QT--day2(信号与槽,多界面跳转)

第一个界面头文件:

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QIcon>  //图标头文件
#include <QPushButton>  //按钮类头文件

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT    //有关信号与槽的元对象

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();


signals:
    void my_signal();
public slots:
    void btn1_slot();//自定义槽函数

private:
    Ui::Widget *ui;  //使用ui界面对应头文件中的命名空间中的类定义的指针

    QPushButton *btn1;
};

#endif // WIDGET_H

第一个界面源文件:

cpp 复制代码
#include "widget.h"       //引入自定义头文件
#include "ui_widget.h"   //引入ui界面的头文件

Widget::Widget(QWidget *parent)
    : QWidget(parent)        //调用父类的有参构造
    , ui(new Ui::Widget)     //构造出ui界面拖拽的成员,并且将地址赋值给ui指针
{
    ui->setupUi(this);    //周用设置界面函数,给ui界面上的组件申请空间
    //设置窗口大小
    this->resize(600,400);
    //设置窗口标题
    this->setWindowTitle("聊天室");
    //设置窗口图标
    this->setWindowIcon(QIcon("E:\\嵌入式\\QT\\图标库\\icon_h8db9qyxft\\QQ.png"));

    //添加按钮
    btn1=new QPushButton("登录",this);
    btn1->resize(100,50);
    btn1->move(400,300);
    btn1->setIcon(QIcon("E:\\photo\\9.jpg"));

    //将按钮1的clicked信号连接到自定义的信号中
    connect(btn1,&QPushButton::clicked,this,&Widget::btn1_slot);
}

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

void Widget::btn1_slot()
{
    //跳转到第二个界面
    emit my_signal();
    //将自身界面关闭
    this->close();
}

第二个界面头文件:

cpp 复制代码
#ifndef SECOND_H
#define SECOND_H

#include <QWidget>

namespace Ui {
class second;
}

class second : public QWidget
{
    Q_OBJECT

public:
    explicit second(QWidget *parent = nullptr);
    ~second();

public slots:
    void jump_slot();

private:
    Ui::second *ui;
};

#endif // SECOND_H

第二个界面源文件:

cpp 复制代码
#include "second.h"
#include "ui_second.h"

second::second(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::second)
{
    ui->setupUi(this);

}

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

void second::jump_slot()
{
    this->show();
}

测试文件:

cpp 复制代码
#include "widget.h"
#include "second.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    second s;
    QObject::connect(&w,&Widget::my_signal,&s,&second::jump_slot);
    return a.exec();
}

效果:

相关推荐
郑州光合科技余经理1 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1231 天前
matlab画图工具
开发语言·matlab
dustcell.2 天前
haproxy七层代理
java·开发语言·前端
norlan_jame2 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone2 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054962 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月2 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237172 天前
C语言-数组练习进阶
c语言·开发语言·算法
Railshiqian2 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript
雪人不是菜鸡2 天前
简单工厂模式
开发语言·算法·c#