用信号和槽实现两个对话框传送数据

whutchao 2013-12-17 03:17:05
最近搞个Qt的项目,用到了两个对话框dialog传送一个字符型的数据,纠结了好多天还是没有琢磨透,在网上也找了好多资料,自己摸索,总算有点了头绪,但是不知道这样好不好,自己感觉似乎存在着问题。
我现在简单的描述下我的demo :
dialog1中有一个lineEdit 三个button

dialog2中只有一个lineEdit:

点击dialong的button1和button2,LineEdit的标签会被设置为1或者2.点击Ok,会生成dialog2对话框,并且dialog2的LineEdit会被设置成dialog1的editline标签。
我实现的代码如下:
dialog1.h
#ifndef DIALOG1_H
#define DIALOG1_H

#include <QDialog>
#include "dialog2.h"

namespace Ui {
class Dialog1;
}

class Dialog1 : public QDialog
{
Q_OBJECT

public:
explicit Dialog1(QWidget *parent = 0);
~Dialog1();
signals:
void selectedButton(QString text) ;
private:
Ui::Dialog1 *ui;
Dialog2 *dlg ;
private slots:
void button1() ;
void button2() ;
void ok() ;

};

#endif // DIALOG1_H

dialog1.cpp
#include "dialog1.h"
#include "ui_dialog1.h"

Dialog1::Dialog1(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog1)
{
ui->setupUi(this);
connect(ui->pushButton1 ,SIGNAL(clicked()) ,this ,SLOT(button1())) ;
connect(ui->pushButton2 ,SIGNAL(clicked()) ,this ,SLOT(button2())) ;
connect(ui->pushButton3 ,SIGNAL(clicked()) ,this ,SLOT(ok())) ;
dlg = new Dialog2() ;
connect(this ,SIGNAL(selectedButton(QString)) ,dlg ,SLOT(updateNum(QString)) ) ;
}

Dialog1::~Dialog1()
{
delete ui;
}


void Dialog1::button1()
{
ui->lineEdit->setText("1");
}


void Dialog1::button2()
{
ui->lineEdit->setText("2");
}

void Dialog1::ok()
{
QString name = ui->lineEdit->text() ;
emit selectedButton(name) ;

// dlg = new Dialog2() ;
// connect(this ,SIGNAL(selectedButton(QString)) ,dlg ,SLOT(updateNum(QString)) ) ;
dlg->show();
}





dialog2.h
#ifndef DIALOG2_H
#define DIALOG2_H

#include <QDialog>

namespace Ui {
class Dialog2;
}

class Dialog2 : public QDialog
{
Q_OBJECT

public:
explicit Dialog2(QWidget *parent = 0);
~Dialog2();

private:
Ui::Dialog2 *ui;


private slots:
void updateNum(QString text) ;
};

#endif // DIALOG2_H



dialog2.cpp
#include "dialog2.h"
#include "ui_dialog2.h"

Dialog2::Dialog2(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog2)
{
ui->setupUi(this);
}

Dialog2::~Dialog2()
{
delete ui;
}

void Dialog2::updateNum(QString text)
{
ui->lineEdit2->setText(text);
}



main.cpp
#include "dialog1.h"
#include "dialog2.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog1 dlg1;
// Dialog2 dlg2 ;

// //信号和槽绑定
// QObject::connect(&dlg1 ,SIGNAL(selectedButton(QString)) ,&dlg2 ,SLOT(updateNum(QString))) ;
dlg1.show();
// dlg2.show();

return a.exec();
}

现在我的问题是:
(1) 这样定义的信号和槽,有没有问题。还有没有其他方法。
(2) 我在dialog1中把dialog2设置为它的成员变量。在构造函数就new了dialog2 ,而在我点击OKbutton时,才把dialog2显示出来,这样是不是在空间上有所浪费呢?
(3) 在
void Dialog1::ok()
{
QString name = ui->lineEdit->text() ;
emit selectedButton(name) ;

// dlg = new Dialog2() ;
// connect(this ,SIGNAL(selectedButton(QString)) ,dlg ,SLOT(updateNum(QString)) ) ;
dlg->show();
}
中,emit信号后,程序怎么运行?

谢谢各位朋友赐教,大家一起学习!!!!
...全文
196 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
killer1978 2013-12-20
  • 打赏
  • 举报
回复
统一回答一下吧, 你要先new 一个对象,以便获得可以操作它的指针, 然后用connect连接对象的信号和槽函数,意识是每次发出这个信号的时候,对应的槽函数会被调用, 在建立信号和槽函数的连接之后,就可以用emit发信号调用槽函数了。 通过信号调用槽函数是Qt特有的机制,创立的初衷可能是为了线程调度的安全,在跨线程操作对象的时候,一般都是通过向对象发信号实现,而不是直接调用那个对象的函数
killer1978 2013-12-18
  • 打赏
  • 举报
回复
引用 2 楼 u011216802 的回复:
[quote=引用 1 楼 killer1978 的回复:] 你是想在2个对话框之间传送数据吧,可以把数据记录在一个全局变量中,两个对话框都可以访问就行了
是的 但是我想发射一个信号实现啊 [/quote] 发信号也可以的,不知道您现在的代码运行起来有什么问题,逻辑我看了下是通顺的,不知道实际运行如何。 您可以单步调试去理解程序的运行状况
whutchao 2013-12-18
  • 打赏
  • 举报
回复
引用 1 楼 killer1978 的回复:
你是想在2个对话框之间传送数据吧,可以把数据记录在一个全局变量中,两个对话框都可以访问就行了
是的 但是我想发射一个信号实现啊
mrx102 2013-12-18
  • 打赏
  • 举报
回复
引用 4 楼 u011216802 的回复:
[quote=引用 3 楼 killer1978 的回复:] [quote=引用 2 楼 u011216802 的回复:] [quote=引用 1 楼 killer1978 的回复:] 你是想在2个对话框之间传送数据吧,可以把数据记录在一个全局变量中,两个对话框都可以访问就行了
是的 但是我想发射一个信号实现啊 [/quote] 发信号也可以的,不知道您现在的代码运行起来有什么问题,逻辑我看了下是通顺的,不知道实际运行如何。 您可以单步调试去理解程序的运行状况[/quote]程序跑起来没有问题啊,我搞不懂这里面的逻辑。特别是:
void Dialog1::ok()
{
    QString name = ui->lineEdit->text() ;
    emit selectedButton(name) ;

//    dlg = new Dialog2() ;
//    connect(this ,SIGNAL(selectedButton(QString)) ,dlg ,SLOT(updateNum(QString)) ) ;
    dlg->show();
}
这里emit selectdButton(name)和dlg = new Dialog2() ; connect(this ,SIGNAL(selectedButton(QString)) ,dlg ,SLOT(updateNum(QString)) ) ;有什么联系,他们为什么都不能写在void Dialog1::ok()函数里面呢 ?谢谢 [/quote] 可以都写在void Dialog1::ok()函数里面,但是必须先创建对象dlg = new Dialog2() 和连接信号\槽 connect(this ,SIGNAL(selectedButton(QString)) ,dlg ,SLOT(updateNum(QString)) ) ; 然后再发送信号emit selectedButton(name);但是这样每次点击OK都会创建对象
whutchao 2013-12-18
  • 打赏
  • 举报
回复
引用 3 楼 killer1978 的回复:
[quote=引用 2 楼 u011216802 的回复:] [quote=引用 1 楼 killer1978 的回复:] 你是想在2个对话框之间传送数据吧,可以把数据记录在一个全局变量中,两个对话框都可以访问就行了
是的 但是我想发射一个信号实现啊 [/quote] 发信号也可以的,不知道您现在的代码运行起来有什么问题,逻辑我看了下是通顺的,不知道实际运行如何。 您可以单步调试去理解程序的运行状况[/quote]程序跑起来没有问题啊,我搞不懂这里面的逻辑。特别是:
void Dialog1::ok()
{
    QString name = ui->lineEdit->text() ;
    emit selectedButton(name) ;

//    dlg = new Dialog2() ;
//    connect(this ,SIGNAL(selectedButton(QString)) ,dlg ,SLOT(updateNum(QString)) ) ;
    dlg->show();
}
这里emit selectdButton(name)和dlg = new Dialog2() ; connect(this ,SIGNAL(selectedButton(QString)) ,dlg ,SLOT(updateNum(QString)) ) ;有什么联系,他们为什么都不能写在void Dialog1::ok()函数里面呢 ?谢谢
killer1978 2013-12-17
  • 打赏
  • 举报
回复
你是想在2个对话框之间传送数据吧,可以把数据记录在一个全局变量中,两个对话框都可以访问就行了

16,213

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧