首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > QT开发 >

QT 两个对话框之间数据传递的有关问题

2012-04-22 
QT 两个对话框之间数据传递的问题QT菜鸟求助,现有两个Dialog对话框,Dialog1里有一个lineedit1和一些数字按

QT 两个对话框之间数据传递的问题
QT菜鸟求助,现有两个Dialog对话框,Dialog1里有一个lineedit1和一些数字按钮(点击按钮能实现将数字显示到lineedit1中),还有一个OK按钮,Dialog2里只有一个lineedit2,怎么将Dialog1里lineedit1的内容在按下OK按钮后显示到Dialog2中的lineedit2中???求大神帮忙,理论的东西不好理解,最好有个小例子的代码,小弟刚学QT一星期,万分感谢!!!

[解决办法]
体力活阿。。。刚才无聊,按你的要求做了个小的project,代码如下:
dialog1.h:
#ifndef DIALOG1_H
#define DIALOG1_H

#include <QDialog>

namespace Ui {
class Dialog1;
}

class Dialog1 : public QDialog
{
Q_OBJECT

public:
explicit Dialog1(QWidget *parent = 0);
~Dialog1();
signals:
void selectedButton(QString text);

private slots:
void button1();
void button2();
void ok();

private:
Ui::Dialog1 *ui;
};

#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->pButton_1,SIGNAL(clicked()),this,SLOT(button1()));
connect(ui->pButton_2,SIGNAL(clicked()),this,SLOT(button2()));
connect(ui->pButton_3,SIGNAL(clicked()),this,SLOT(ok()));
}

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

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

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

void Dialog1::ok()
{
emit selectedButton(ui->lineEdit1->text());
}

dialog1.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog1</class>
 <widget class="QDialog" name="Dialog1">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>224</width>
<height>104</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog1</string>
</property>
<widget class="QLineEdit" name="lineEdit1">
<property name="geometry">
<rect>
<x>90</x>
<y>20</y>
<width>113</width>
<height>27</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>30</x>
<y>20</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>number</string>
</property>
</widget>
<widget class="QPushButton" name="pButton_1">
<property name="geometry">
<rect>
<x>10</x>
<y>60</y>
<width>61</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>1</string>
</property>
</widget>
<widget class="QPushButton" name="pButton_2">
<property name="geometry">
<rect>
<x>80</x>
<y>60</y>
<width>61</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>2</string>


</property>
</widget>
<widget class="QPushButton" name="pButton_3">
<property name="geometry">
<rect>
<x>150</x>
<y>60</y>
<width>61</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>ok</string>
</property>
</widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

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 slots:
void updateNum(QString text);

private:
Ui::Dialog2 *ui;
};

#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);
}

dialog2.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog2</class>
 <widget class="QDialog" name="Dialog2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>225</width>
<height>67</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>58</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>NUM</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit2">
<property name="geometry">
<rect>
<x>90</x>
<y>20</y>
<width>113</width>
<height>27</height>
</rect>
</property>
</widget>
 </widget>
 <resources/>
 <connections/>
</ui>

main.cpp:
#include <QtGui/QApplication>
#include "dialog1.h"
#include "dialog2.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog1 dialog1;
Dialog2 dialog2;

QObject::connect(&dialog1,SIGNAL(selectedButton(QString)),&dialog2,SLOT(updateNum(QString)));

dialog1.show();
dialog2.show();

return a.exec();
}

dialogs.pro:
#-------------------------------------------------
#
# Project created by QtCreator 2011-08-25T15:09:35
#
#-------------------------------------------------

QT += core gui

TARGET = dialogs
TEMPLATE = app


SOURCES += main.cpp\
dialog1.cpp \
dialog2.cpp

HEADERS += dialog1.h \
dialog2.h

FORMS += dialog1.ui \
dialog2.ui


热点排行