赞
踩
1、打开vs,新建一个项目,双击.ui文件,进入UI设计器界面,进行主窗口的设计,设计结果如下:
2、重新添加一个.ui文件,用于子窗口的设计(具体请参照往期创作的文章,这里不再赘述),设计结果如下图:添加一个label组件
3、在vs中添加相关代码,具体如下:
(1)在test4.h中,代码如下:
- #pragma once
-
- #include <QtWidgets/QMainWindow>
- #include "ui_test4.h"
- #include "NWindows.h"
-
- class test4 : public QMainWindow
- {
- Q_OBJECT
-
- public:
- test4(QWidget *parent = Q_NULLPTR);
- NWindows *w;
-
- private:
- Ui::test4Class ui;
-
- private slots:
- void onpushButton();
- };

(2)在test4.cpp中,代码如下:
- #include "test4.h"
- #include "ui_NWindows.h"
-
-
- QString globaldata; //声明一个全局变量用于窗体间传值
-
- test4::test4(QWidget *parent)
- : QMainWindow(parent)
- {
- ui.setupUi(this);
- connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(onpushButton()));
- }
-
- void test4::onpushButton()
- {
- globaldata = ui.textEdit->toPlainText(); //获取lineEdit输入的值赋值给全局变量
- NWindows *w = new NWindows;
- w->show();
- }

(3)在NWindows.h中,代码如下:
- #pragma once
-
- #include <QWidget>
- #include "ui_NWindows.h"
-
- class NWindows : public QWidget
- {
- Q_OBJECT
-
- public:
- NWindows(QWidget *parent = Q_NULLPTR);
- ~NWindows();
-
- private:
- Ui::NWindows ui;
- };

(4)在NWindows.cpp中,代码如下:
- #include "NWindows.h"
-
- extern QString globaldata; //声明外部变量
-
- NWindows::NWindows(QWidget *parent)
- : QWidget(parent)
- {
- ui.setupUi(this);
- ui.label->setText(globaldata); //对全局变量进行显示
- }
-
- NWindows::~NWindows()
- {
-
- }
4、运行结果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。