Hello!

I am writing code in Qt Creator.

5 hours I suffer with a question:

How to transfer data from the dialog to the main window (MainWindow) at the touch of a button? I read a bunch of material and still can not understand.

Task: Pressing the start_btn button opens a dialog. It has a form. When you click the OK button, the data is transferred to the MainWindow and the dialog closes.

Here is the mainwindow.cpp code:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->label->hide(); connect(ui->start_btn,SIGNAL(clicked(bool)), this, SLOT(open_createdialog())); CreateDialog *d; connect(d,SIGNAL(sendData(QString,QString)),this,SLOT(set_data(QString,QString))); } MainWindow::~MainWindow() { delete ui; } void MainWindow::open_createdialog() { CreateDialog d; d.exec(); } void MainWindow::set_data(QString name, QString Yname) { qDebug() << name; } 

This is code createdialog.cpp

  CreateDialog::CreateDialog(QWidget *parent) : QDialog(parent), ui(new Ui::CreateDialog) { ui->setupUi(this); connect(ui->ok_btn, SIGNAL(clicked()), this, SLOT(onSendData())); } CreateDialog::~CreateDialog() { delete ui; } void CreateDialog::onSendData() { emit sendData( ui->lineEdit->text(), ui->lineEdit_2->text() ); } 

When you start it says: The program ended unexpectedly.

Thanks in advance for your help!

    1 answer 1

    Problem solved. Made connection in function of creation of a window and transferred the link to object through &.

    I give the code:

     void MainWindow::open_createdialog() { CreateDialog d; connect(&d,SIGNAL(sendData(QString,QString)),this,SLOT(set_data(QString,QString))); d.exec(); }