part 3 Qt5.5.0窗口之间传值(一)
一、信号与槽机制传值
1、主窗口与子窗口之间传值:
主窗口为ParentChild,继承MainWindow类
子窗口为TestDialog,继承QDialog类
1)TestDialog:
(1)testdialog.h
#ifndef TESTDIALOG_H
#define TESTDIALOG_H
#include <QtWidgets/QDialog>
#include "ui_testdialog.h"
//namespace Ui{
// class TestDialog;
//}
class TestDialog : public QDialog
{
Q_OBJECT
public:
TestDialog(QWidget *parent = 0);
~TestDialog();
signals:
void dlgReturn(/*int*/QString);
private slots:
void pushButton_clicked();
private:
Ui::TestDialog ui;
};
#endif // TESTDIALOG_H
(2)、testdialog.cpp
#include "testdialog.h"
TestDialog::TestDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
connect(ui.on_pushButton_ok, SIGNAL(clicked()), this, SLOT(pushButton_clicked()));//确定按钮得信号槽函数,点击后发射信号dlgReturn(int or Qstring)
}
TestDialog::~TestDialog()
{
}
void
TestDialog::pushButton_clicked()
{
emit dlgReturn(ui.lineEdit->text()/*.toInt()*/);
//close();
}
2)ParentChild:
(1)parentchild.h
#ifndef PARENTCHILD_H
#define PARENTCHILD_H
#include <QtWidgets/QMainWindow>
#include "ui_parentchild.h"
#include "testdialog.h"
#include "ui_testdialog.h"
#include <qlabel.h>
#include <qstring.h>
namespace Ui{
class ParentChild;
}
class ParentChild : public QMainWindow
{
Q_OBJECT
public:
ParentChild(QWidget *parent = 0);
~ParentChild();
private slots:
void showValue(QString/*int*/);
public slots:
void on_pushButton_clicked();
private:
Ui::ParentChildClass ui;
};
#endif // PARENTCHILD_H
(2)parentchild.cpp
#include "parentchild.h"
#include "ui_parentchild.h"
#include "testdialog.h"
ParentChild::ParentChild(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
this->setWindowTitle(QString::fromLocal8Bit("窗口传值测试"));
//关联信号和槽函数
connect(ui.pushButton_Dlg, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));//主窗口信号槽函数,点击后弹出对话框,连接子窗口的发射信号,同<span style="white-space:pre"> </span> //时触发显示函数showValue(int or Qstring)
}
ParentChild::~ParentChild()
{
}
void
ParentChild::showValue(/*int*/QString val)
{
QString str = QString("%1").arg(val);
ui.label->setText(str);
}
void
ParentChild::on_pushButton_clicked()
{
TestDialog *dlg = new TestDialog(this);
dlg->setModal(false);//非模态为false,模态为true
connect(dlg, SIGNAL(dlgReturn(QString/*int*/)), this, SLOT(showValue(QString/*int*/)));//连接信号与槽
dlg->show();
}
3)main.cpp
这个不用做任何更改
#include "parentchild.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ParentChild w;
w.show();
return a.exec();
}
运行:
二、Public函数传值
1、子窗口向主窗口传值
主窗口为ParentChild,继承MainWindow类
子窗口为TestDialog,继承QDialog类
该程序先弹出子窗口,然后传值给主窗口
1)TestDialog子窗口
子窗口中通过按钮来触发传参
(1)testdialog.h
#ifndef TESTDIALOG_H
#define TESTDIALOG_H
#include <QtWidgets/QDialog>
#include "ui_testdialog.h"
class TestDialog : public QDialog
{
Q_OBJECT
public:
TestDialog(QWidget *parent = 0);
~TestDialog();
private slots:
void pushButton_clicked();
private:
Ui::TestDialog ui;
};
#endif // TESTDIALOG_H
(2)testdialog.cpp
#include "testdialog.h"
/
#include "ui_parentchild.h"
#include "parentchild.h"
/
TestDialog::TestDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
connect(ui.pushButton_ok, SIGNAL(clicked()), this, SLOT(pushButton_clicked()));
}
TestDialog::~TestDialog()
{
}
void
TestDialog::pushButton_clicked()
{
ParentChild *w = new ParentChild;
w->dlgReturn(ui.lineEdit->text());
w->show();
}
2)ParentChild主窗口
主窗口中没有信号槽触发函数
(1)parentchild.h
#ifndef PARENTCHILD_H
#define PARENTCHILD_H
#include <QtWidgets/QMainWindow>
#include "ui_parentchild.h"
#include "testdialog.h"
#include "ui_testdialog.h"
class ParentChild : public QMainWindow
{
Q_OBJECT
public:
ParentChild(QWidget *parent = 0);
~ParentChild();
public:
void dlgReturn(QString data);
private:
Ui::ParentChild ui;
};
#endif // PARENTCHILD_H
(2)parentchild.cpp
#include "parentchild.h"
ParentChild::ParentChild(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}
ParentChild::~ParentChild()
{
}
void
ParentChild::dlgReturn(QString data)
{
ui.label->setText(data);
ui.lineEdit_1->setText(data);
}
3)main.cpp
这个main.cpp中的代码是首先弹出子窗口来传值的,注意下,头文件中要加入子窗口头文件,主窗口的头文件可以注释也可以保留
/
//#include "parentchild.h"
#include "testdialog.h"
/
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//ParentChild w;
TestDialog w;
w.show();
return a.exec();
}
运行: