1 Star 11 Fork 6

胡歌-此生不换/Qt-Project

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Qt 信号和槽

当我们需要一个界面通知另一个界面时,可以采用信号和槽机制。通过链接信号和槽,当一个界面发送信号时,链接该信号的槽会被响应,从而达到消息传递的目的。

1. 不同的连接方式

qt4 好像用的不太行:

    connect(ui->button, SIGNAL(clicked(bool)), this, SLOT(showChildDialogSlot()));

就用 qt5 的方式了:

    connect(ui->button, &QPushButton::clicked, this, &MainWindow::showChildDialogSlot);

2. 实现界面的切换

2.1 MainWindow 隐藏,child_dialog_ 显示

最开始的时候肯定是只有我们的 MainWindow 窗口的,所以我们 MainWindow 窗口设置了一个 QPushButton 的点击事件来触发 void showChildDialogSlot(),隐藏自己,展示 childDialog* child_dialog_

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QDialog>
#include "childdialog.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public slots:
    void showChildDialogSlot();

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

private:
    Ui::MainWindow *ui;
    childDialog* child_dialog_;
};
#endif // MAINWINDOW_H

mainwindow.cpp:

void MainWindow::showChildDialogSlot(){
// 设置成成员变量了
//    QDialog* child_dialog_ = new QDialog(this);
//    child_dialog_->show();

    this->hide();
    child_dialog_->show();
}

2.2 child_dialog_ 隐藏,MainWindow 显示

这个时候就只有 childDialog* child_dialog_,也设置一个 QPushButton 的点击事件来触发 void showMainWindowSlot(),隐藏自己,给 QWidget* parent_(MainWindow) 发送信号 emit showMainWindowSignal() 接收到信号后显示自己就可以了。

childdialog.h:

#ifndef CHILDDIALOG_H
#define CHILDDIALOG_H

#include <QDialog>
#include <QWidget>

namespace Ui {
class childDialog;
}

class childDialog : public QDialog // 注意继承的是 QDialog 不是 QWidget......
{
    Q_OBJECT

signals:
    void showMainWindowSignal();

public slots:
    void showMainWindowSlot();

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

private:
    Ui::childDialog *ui;

    // 1. 存储一下父窗口:可以设置对应的 showMainWindowSignal 和 showMainWindowSlot 给父窗口通信
    // 2. 父窗口的构造函数里面设置对应的我们这个的 childDialog* child_dialog_; 再 connect(child_dialog_, &childDialog::showMainWindowSignal, this, &MainWindow::showMainWindow);
    // 3. 这个界面设置一个 PushButton, 点击触发 showMainWindowSlot 函数向父窗口 emit showMainWindowSignal();
    QWidget *parent_;
};

#endif // CHILDDIALOG_H

childdialog.cpp:

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

    // 自己的信号 连接 自己的槽函数
    connect(ui->pushButton, &QPushButton::clicked, this, &childDialog::showMainWindowSlot);
}

void childDialog::showMainWindowSlot(){
    this->hide();
    emit showMainWindowSignal();
}

mainwindow.h 文件添加下面的公有槽函数:

    void showMainWindowSlot();

mainwindow.cpp 文件添加 child_dialog_ 的信号和 MainWindow 的槽函数的连接,以及 showMainWindowSlot 槽函数的实现:

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

//    定义了 child_dialog_ 为 MainWindow 的私有成员变量
    child_dialog_ = new childDialog(this);

    // showDialog 函数的参数个数少于 clicked 函数的参数个数就可以了
//    connect(ui->button, SIGNAL(clicked(bool)), this, SLOT(showDialog()));

    // 自己的信号 连接 自己的槽函数
    connect(ui->button, &QPushButton::clicked, this, &MainWindow::showChildDialogSlot);
    // connect(ui->button, &QPushButton::clicked, this, &childDialog::showChildDialogSlot); // 自己的信号 连接 别人的槽函数 error
    // connect(ui->button, &QPushButton::clicked, this, &child_dialog_->showChildDialogSlot); // 自己的信号 连接 别人的槽函数 error


    // 别人的信号 连接 自己的槽函数
    connect(child_dialog_, &childDialog::showMainWindowSignal, this, &MainWindow::showMainWindowSlot);
}

void MainWindow::showMainWindowSlot(){
    this->show();
    child_dialog_->hide(); // 可以不写,因为 MainWindow 在接收信号之前,child_dialog_ 已经把自己给 this->hide() 之后,才给 MainWindow 发送信号的。
}

以上就实现了界面的来回切换。

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ax020913/qt-project.git
git@gitee.com:ax020913/qt-project.git
ax020913
qt-project
Qt-Project
master

搜索帮助