day11 coding Qt案例: 切换AB界面

This commit is contained in:
flykhan 2023-06-27 22:44:31 +08:00
parent 5bcb4c519d
commit f36cc5ce97
6 changed files with 160 additions and 0 deletions

42
day11/switchab/awidget.cpp Executable file
View File

@ -0,0 +1,42 @@
#include "awidget.h"
aWidget::aWidget(QWidget *parent)
: QWidget(parent)
{
// 设置A窗口实例的属性
setFixedSize(400,200);
setWindowTitle("A界面");
move(200,100);
// 新建B窗口对象
b = new bWidget();
b->setFixedSize(400,200);
b->setWindowTitle("B界面");
b->move(800,100);
// 新建“进入B”按钮并设置其属性
btnToB = new QPushButton("进入B",this);
btnToB->resize(60,40);
// 绑定“进入B”按钮点击信号与“进入B界面并隐藏A界面”的槽函数
connect(btnToB,&QPushButton::clicked,this,&aWidget::toBHandle);
// 绑定“B界面返回A界面信号”与当前界面的“A界面显示并隐藏B界面”的槽函数
connect(b,&bWidget::returnASignal,this,&aWidget::returnAHandle);
}
void aWidget::toBHandle()
{
this->hide(); // 隐藏A界面
b->show(); // 显示B界面
}
void aWidget::returnAHandle()
{
b->hide(); // 隐藏B界面
this->show(); // 显示A界面
}
aWidget::~aWidget()
{
}

32
day11/switchab/awidget.h Executable file
View File

@ -0,0 +1,32 @@
#ifndef AWIDGET_H
#define AWIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QDebug>
#include "bwidget.h"
class aWidget : public QWidget
{
Q_OBJECT
private:
bWidget *b;
public slots:
void toBHandle();
void returnAHandle();
signals:
void toBSignal();
private:
QPushButton *btnToB;
public:
aWidget(QWidget *parent = nullptr);
~aWidget();
};
#endif // AWIDGET_H

16
day11/switchab/bwidget.cpp Executable file
View File

@ -0,0 +1,16 @@
#include "bwidget.h"
bWidget::bWidget(QWidget *parent) : QWidget(parent)
{
// 新建“返回A”按钮并设置其属性
btnReturnA = new QPushButton("返回A",this);
btnReturnA->resize(60,40);
// 绑定“返回A”按钮点击事件与“发送返回A界面的信号”的槽函数
connect(btnReturnA,&QPushButton::clicked,this,&bWidget::sendReturnASignal);
}
void bWidget::sendReturnASignal()
{
emit returnASignal(); // 触发“返回A界面”信号
}

28
day11/switchab/bwidget.h Executable file
View File

@ -0,0 +1,28 @@
#ifndef BWIDGET_H
#define BWIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QDebug>
class bWidget : public QWidget
{
Q_OBJECT
private:
QPushButton *btnReturnA;
public slots:
void sendReturnASignal();
signals:
void returnASignal();
public:
explicit bWidget(QWidget *parent = nullptr);
signals:
};
#endif // BWIDGET_H

12
day11/switchab/main.cpp Executable file
View File

@ -0,0 +1,12 @@
#include "awidget.h"
#include "bwidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication qa(argc, argv);
aWidget a; // 新建一个界面A对象a
a.show(); // 程序初始化运行时显示A对象a界面
return qa.exec();
}

30
day11/switchab/switchab.pro Executable file
View File

@ -0,0 +1,30 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
bwidget.cpp \
main.cpp \
awidget.cpp
HEADERS += \
awidget.h \
bwidget.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target