一个构造函数中带有参数的Dialog 窗体通过信号槽机制传给MainWindow 信号,这为什么不可以呢?该怎么做呢?
直接上代码吧(这是个不带参数的Dialog,能运行):
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "dialog.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void Receive_Slot();
private:
Ui::MainWindow *ui;
Dialog dialog;
};
#endif // MAINWINDOW_H
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
signals:
void To_MainWindow_Signal();
private slots:
void on_pushButton_clicked();
private:
Ui::Dialog *ui;
int num;
};
#endif // DIALOG_H
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_clicked()
{
emit To_MainWindow_Signal();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&dialog,SIGNAL(To_MainWindow_Signal()),this,SLOT(Receive_Slot()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
dialog.exec();
}
void MainWindow::Receive_Slot()
{
ui->label->setText("Hello World !");
}
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
这个程序能运行,并能实现,Dialog向MainWindow传递信息,但如果Dialog是含有参数,例如 explicit Dialog(int num,QWidget *parent = 0); ,该怎么办呢?求大神解答下。谢谢。
Qt?Slot?Signal
------解决方案--------------------
声明Dialog的指针 通过new创建对话框