16,213
社区成员
发帖
与我相关
我的任务
分享
#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
#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();
}
#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
#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);
}
#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();
}
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信号后,程序怎么运行?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都会创建对象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()函数里面呢 ?谢谢