1、父窗口和子窗口名称介绍
父窗口:Test_7
子窗口:testnew
2、父窗口与子窗口内容介绍
子窗口:声明信号、发送信号
父窗口:声明槽函数,定义槽函数,connect连接
3、在父窗口中(Test_7.cpp),完整代码如下:
#include "Test_7.h"
Test_7::Test_7(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
w = new TestNew;
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(OnpushButton()));
connect(w, SIGNAL(SignalSendQString_1(QString)), this, SLOT(SlotGetQString_1(QString)));
}
void Test_7::SlotGetQString_1(QString str)
{
ui.textEdit->insertPlainText(str);
}
void Test_7::OnpushButton()
{
w->show();
}
4、父窗口中(Test_7.h),完整代码如下:
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_Test_7.h"
#include "testnew.h"
class Test_7 : public QMainWindow
{
Q_OBJECT
public:
Test_7(QWidget *parent = Q_NULLPTR);
private:
Ui::Test_7Class ui;
TestNew *w;
private slots:
void SlotGetQString_1(QString str);
void OnpushButton();
};
5、子窗口(testnew.cpp),完整代码如下:
#include "testnew.h"
TestNew::TestNew(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
connect(ui.pushButton,SIGNAL(clicked()), this, SLOT(OnpushButton()));
}
TestNew::~TestNew()
{
}
void TestNew::OnpushButton()
{
QString str = ui.textEdit->toPlainText();
emit SignalSendQString_1(str);
}
6、子窗口(testnew.h),完整代码如下:
#pragma once
#include <QWidget>
#include "ui_testnew.h"
class TestNew : public QWidget
{
Q_OBJECT
public:
TestNew(QWidget *parent = Q_NULLPTR);
~TestNew();
signals:
void SignalSendQString_1(QString);
private:
Ui::TestNew ui;
private slots:
void OnpushButton();
};
7、程序运行结果,如下:
文章来源:https://www.toymoban.com/news/detail-510868.html
文章来源地址https://www.toymoban.com/news/detail-510868.html
到了这里,关于Qt子窗口给父窗口传值(信号与槽机制)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!