代码拉取完成,页面将自动刷新
Qt
信号和槽当我们需要一个界面通知另一个界面时,可以采用信号和槽机制。通过链接信号和槽,当一个界面发送信号时,链接该信号的槽会被响应,从而达到消息传递的目的。
qt4
好像用的不太行:
connect(ui->button, SIGNAL(clicked(bool)), this, SLOT(showChildDialogSlot()));
就用 qt5
的方式了:
connect(ui->button, &QPushButton::clicked, this, &MainWindow::showChildDialogSlot);
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();
}
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 发送信号的。
}
以上就实现了界面的来回切换。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。