项目文件 .pro
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 += \
main.cpp \
otherwidget.cpp \
widget.cpp
HEADERS += \
otherwidget.h \
widget.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
主窗口头文件
Widgets.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QPushButton>
#include"otherwidget.h"
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
QPushButton b;
QPushButton*ob;
QPushButton son_widget;
otherWidget son;
//槽函数
void Get_to();
void changeWin();//在主窗口类中实现这个成员槽函数,是因为当主窗口向父窗口转换时,是主窗口类来调用函数,而且此时也只有主窗口包含一个成员父窗口对象
void resume();
};
#endif // WIDGET_H
------------------------------------------------------------------------------------------
副窗口头文件
otherwidgets.h
#ifndef OTHERWIDGET_H
#define OTHERWIDGET_H
#include <QWidget>
#include<QPushButton>
class otherWidget : public QWidget
{
Q_OBJECT
public:
explicit otherWidget(QWidget *parent = nullptr);
signals:
/*
* 信号必须有signals关键字来声明
* 信号没有返回值,但是可以有参数
* 信号就是函数声明,但是只用申明,无需定义
* 使用:emit mySignals()
*/
void mySignals();
private:
QPushButton five;
void son_do_sth();
};
#endif // OTHERWIDGET_H
---------------------------------------------------------------------------------------------------------------------------
副窗口实现文件
otherwidget.cpp
#include "otherwidget.h"
#include<QPushButton>
otherWidget::otherWidget(QWidget *parent) : QWidget(parent)
{
setWindowTitle("上流社会zoom");
five.setParent(this);
five.setText("手拿高脚杯");
connect(&five,&QPushButton::clicked,this,&otherWidget::son_do_sth);
//固定副窗口大小
resize(400,300);
}
/*
* 从这里可以明白了,操作的权利只能存在于主窗口,副窗口只拥有向主窗口发设信号的权力,然后有主窗口接收信号,从而有主窗口完成操作
*/
void otherWidget::son_do_sth()
{
emit mySignals();
}
副窗口头文件
otherwidgets.h
#ifndef OTHERWIDGET_H
#define OTHERWIDGET_H
#include <QWidget>
#include<QPushButton>
class otherWidget : public QWidget
{
Q_OBJECT
public:
explicit otherWidget(QWidget *parent = nullptr);
signals:
/*
* 信号必须有signals关键字来声明
* 信号没有返回值,但是可以有参数
* 信号就是函数声明,但是只用申明,无需定义
* 使用:emit mySignals()
*/
void mySignals();
private:
QPushButton five;
void son_do_sth();
};
#endif // OTHERWIDGET_H
副窗口实现文件
otherwidget.cpp
#include "otherwidget.h"
#include<QPushButton>
otherWidget::otherWidget(QWidget *parent) : QWidget(parent)
{
setWindowTitle("上流社会zoom");
five.setParent(this);
five.setText("手拿高脚杯");
connect(&five,&QPushButton::clicked,this,&otherWidget::son_do_sth);
//固定副窗口大小
resize(400,300);
}
/*
* 从这里可以明白了,操作的权利只能存在于主窗口,副窗口只拥有向主窗口发设信号的权力,然后有主窗口接收信号,从而有主窗口完成操作
*/
void otherWidget::son_do_sth()
{
emit mySignals();
}
主窗口实现文件
Widgets.cpp
#include "widget.h"
#include<QPushButton>
#include"otherwidget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
b.setParent(this);
b.setText((QString("关闭")));
b.move(100,100);
ob=new QPushButton(this);
ob->setText(QString("高翻,永远的神!"));
connect(&b,&QPushButton::pressed,this,&Widget::close);
//解释:
/*
*&b是信号发出者,指针类型
*&QPushButton::pressed是处理的信号 ,&发送者的类名::信号名字
* this:信号接收者
* &Widget::close是槽函数,信号处理函数 &接收的类名::槽函数的名字
*/
/*自定义槽,普通函数的用法
* QT5:任意的成员函数,普通全局变量,静态函数
* 槽函数需要与信号一致(参数,返回值)
* 由于信号都没有返回值,说明槽函数一定没有返回值
*/
//Get_to函数是我自定义的一个修改按钮显示情况的一个槽函数
connect(ob,&QPushButton::released,this,&Widget::Get_to);
//在点击完其中一个按钮之后,信号的接收者变成了另一个按钮,槽函数是把另一个按钮给隐藏
connect(ob,&QPushButton::released,&b,&QPushButton::hide);
/*
* 类比一下的话
* 信号就是短信
* 槽函数就是接受的手机
* 一条短信可以发给发给多个手机
*/
//主窗口命名
setWindowTitle("我是上单zoom");
//重新设置一个按钮
son_widget.setParent(this);
son_widget.setText("身穿燕尾服");
son_widget.move(50,50);
//如果直接显示子窗口,那么会得到父子窗口重叠的两个窗口
connect(&son_widget,&QPushButton::released,this,&Widget::changeWin);//操蛋的BUG出现在第三个参数,因为是从主窗口向父窗口转换,所以消息的接收者是主窗口
//处理从子窗口发射来的信号
connect(&son,&otherWidget::mySignals,this,&Widget::resume);
//固定主窗口大小
resize(400,300);
}
Widget::~Widget()
{
delete ob;
}
void Widget::Get_to()
{
ob->setText(QString("高翻,爆发的神!"));
}
void Widget::changeWin()
{
//隐藏主窗口
this->hide();
//显示副窗口
son.show();
}
void Widget::resume()
{
this->show();
son.hide();
}
主函数文件
Main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
项目运行
点击按钮“身穿燕尾服”
再次点击按钮“手拿高脚杯”
两个按钮的点击,可以使得主副窗口来回切换,并且所有行为都是主窗口主导,副窗口只能来发送信息,再由主窗口获取信息执行行为。