QT入门(三) 多窗体之间的数据传送

因为我用的是vs的编译器,所以网上例子很少,结合qt自带的编译器的例子,尝试了半天,实现从子窗体向主窗体的数据传递,

主窗体.h代码如下

#ifndef ALGORITHMREALIZEPLATFORM_H
#define ALGORITHMREALIZEPLATFORM_H

#include <QtWidgets/QMainWindow>
#include "ui_MainUI.h"
#include "childwindow.h"

class AlgorithmRealizePlatform : public QMainWindow
{
	Q_OBJECT

public:
	AlgorithmRealizePlatform(QWidget *parent = 0);
	~AlgorithmRealizePlatform();
signals:
	void sendsignal(QString);

private:
	Ui::AlgorithmRealizePlatformClass ui;
	childwindow CW;
	QPushButton QPB;
private slots:
	void reshow(QString);
	void test();
};

#endif // ALGORITHMREALIZEPLATFORM_H

主窗体.cpp文件

#include "algorithmrealizeplatform.h"

AlgorithmRealizePlatform::AlgorithmRealizePlatform(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
    //普通的窗体内部信号和槽体的连接
	connect(&QPB, &QPushButton::clicked, this, &AlgorithmRealizePlatform::test); 
    //与子窗体的信号连接      
	connect(&CW, &childwindow::send_data, this, &AlgorithmRealizePlatform::reshow);
}

AlgorithmRealizePlatform::~AlgorithmRealizePlatform()
{
	
}

void AlgorithmRealizePlatform::test()
{
	QString a = "aaaaaaaaaa";
	ui.label->setText(a);
	CW.show();
}

void AlgorithmRealizePlatform::reshow(QString y)
{
	QString a = "aaaaaaaaaa";
	ui.label->setText(y);
	//this->close();
}

 子窗体.h文件

#ifndef CHILDWINDOW_H
#define CHILDWINDOW_H

#include <QWidget>
#include "ui_childwindow.h"

class childwindow : public QWidget
{
	Q_OBJECT

public:
	childwindow(QWidget *parent = 0);
	~childwindow();
	QString buf;
	
signals:
	void send_data(QString y);

private:
	Ui::childwindow ui;
private slots:
	void on_push_close_clicked();
};

#endif // CHILDWINDOW_H

子窗体.cpp文件

#include "childwindow.h"

childwindow::childwindow(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
}

childwindow::~childwindow()
{

}

void childwindow::on_push_close_clicked()
{
	buf = ui.label->text();
	emit send_data(buf);    //传出子窗体信号
	this->close();
}

参照https://blog.csdn.net/weibo1230123/article/details/79116016改写的,

这里,我对connect(sender, SIGNAL(signal), receiver, SLOT(slot))的理解是:

1、sender和receiver分别是信号端和接收端的QObject的实例化,signal和slot是其中的具体方法

2、需要注意的是signal和slot的参数需要尽量一致吧,不要找麻烦,想继续的话,转https://blog.csdn.net/WilliamSun0122/article/details/72810352

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值