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

版权声明: https://blog.csdn.net/qq_25147107/article/details/83991372

因为我用的是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

猜你喜欢

转载自blog.csdn.net/qq_25147107/article/details/83991372