1、在vs中创建一个新项目,并设计UI界面如下:(主窗口含有一个treewidget、一个textEdit、一个pushButton;子窗口含有一个lineEdit、一个pushButton)
2、需实现子窗口传值给主窗口,并显示在第二个文本框(textEdit)中 。因此,需要在子窗口中写好信号,并发送信号;在主窗口中,需要添加槽函数,以及connect连接。
(1)在子窗口中需要添加信号并发射信号,代码如下:
signals:
void SignalSend(QString);
void TestNew_6::OnpushButton()
{
QString str = ui.lineEdit->text();
emit SignalSend(str);
}
(2)在主窗口中添加槽函数及信号与槽的连接,并实现槽函数,代码如下:
private slots:
void SlotGet(QString);
w = new TestNew_6;
connect(w,SIGNAL(SignalSend(QString)),this,SLOT(SlotGet(QString)));
void Test_6::SlotGet(QString str)
{
ui.textEdit->insertPlainText(str);
QTreeWidgetItem * li11 = new QTreeWidgetItem(QStringList() <<str);
li8->addChild(li11);
}
(3)在主函数的头文件中需包含如下文件:
#include "TestNew_6.h"
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QMessageBox>
#include <QDebug>
#include <QStyleFactory>
3、下面展示项目的完整代码:
(1)在子窗口的头文件中添加如下代码:
#include "TestNew_6.h"
TestNew_6::TestNew_6(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(OnpushButton()));
}
TestNew_6::~TestNew_6()
{
}
void TestNew_6::OnpushButton()
{
QString str = ui.lineEdit->text();
emit SignalSend(str);
}
(2)在子窗口的源文件中添加如下代码:
#pragma once
#include <QWidget>
#include "ui_TestNew_6.h"
class TestNew_6 : public QWidget
{
Q_OBJECT
public:
TestNew_6(QWidget *parent = Q_NULLPTR);
~TestNew_6();
signals:
void SignalSend(QString);
private:
Ui::TestNew_6 ui;
private slots:
void OnpushButton();
};
(3)在主窗口的头文件中添加如下代码:
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_Test_6.h"
#include "TestNew_6.h"
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QMessageBox>
#include <QDebug>
#include <QStyleFactory>
#include "TestNew_6.h"
class Test_6 : public QMainWindow
{
Q_OBJECT
public:
Test_6(QWidget *parent = Q_NULLPTR);
private:
Ui::Test_6Class ui;
TestNew_6 *w;
QTreeWidgetItem *li8;
private slots:
void SlotGet(QString);
void OnpushButton();
};
(4)在主窗口的源文件中添加如下代码:
#include "Test_6.h"
Test_6::Test_6(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
w = new TestNew_6;
connect(w,SIGNAL(SignalSend(QString)),this,SLOT(SlotGet(QString)));
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(OnpushButton()));
ui.treeWidget->setStyle(QStyleFactory::create("windows")); //设置虚线和伸缩加减号
ui.treeWidget->clear(); //QTreeWidget清空
ui.treeWidget->setHeaderLabels(QStringList() << QStringLiteral("名称") << QStringLiteral("编号"));
ui.treeWidget->setColumnCount(2); //tree widget的列数
//顶层节点设计
QTreeWidgetItem * liItem = new QTreeWidgetItem(QStringList() << QStringLiteral("项目"));
ui.treeWidget->addTopLevelItem(liItem);
//子节点的设置
QTreeWidgetItem * li1 = new QTreeWidgetItem(QStringList() << QStringLiteral("参数"));
liItem->addChild(li1);
QTreeWidgetItem * li2 = new QTreeWidgetItem(QStringList() << QStringLiteral("几何"));
liItem->addChild(li2);
QTreeWidgetItem * li3 = new QTreeWidgetItem(QStringList() << QStringLiteral("网络"));
liItem->addChild(li3);
QTreeWidgetItem * li4 = new QTreeWidgetItem(QStringList() << QStringLiteral("集合"));
liItem->addChild(li4);
QTreeWidgetItem * li5 = new QTreeWidgetItem(QStringList() << QStringLiteral("材料"));
liItem->addChild(li5);
QTreeWidgetItem * li6 = new QTreeWidgetItem(QStringList() << QStringLiteral("属性"));
liItem->addChild(li6);
QTreeWidgetItem * li7 = new QTreeWidgetItem(QStringList() << QStringLiteral("载荷"));
liItem->addChild(li7);
li8 = new QTreeWidgetItem(QStringList() << QStringLiteral("约束"));
liItem->addChild(li8);
QTreeWidgetItem * li9 = new QTreeWidgetItem(QStringList() << QStringLiteral("求解"));
liItem->addChild(li9);
QTreeWidgetItem * li10 = new QTreeWidgetItem(QStringList() << QStringLiteral("结果"));
liItem->addChild(li10);
}
void Test_6::OnpushButton()
{
w->show();
}
void Test_6::SlotGet(QString str)
{
ui.textEdit->insertPlainText(str);
QTreeWidgetItem * li11 = new QTreeWidgetItem(QStringList() <<str);
li8->addChild(li11);
}
4、运行结果,子窗口的输入会传送到主窗口的第二个文本框内,并在第一个文本框中实现子节点的添加,子节点名称即为子窗口中输入的内容。具体效果如下: