Search code examples
c++qtdata-transferqdialog

pass string from dialog to MainWindow?


I'm trying to make a program which has MainWindow.cpp and a dialog1.cpp, I need to pass a string from QLineEdit of my dialog to a function in MainWindow.cpp.Till now I have just made a qt dialog form class and used modal approach to view the dialog.
The dialog gets input from lineedit in a myString.
Dialog.cpp

    void Dialog::on_buttonBox_clicked(QUrl fileUrl)
{
    QString myString = ui->lineEdit->text();
}

Now I have to transfer the data in myString to dnldFile function as parameter which is in MainWindow.cpp.

    void MainWindow::on_dnldButton_clicked()
{
    Dialog newDnld;
    newDnld.setModal(true);
    newDnld.exec();
    dnldFile();
}

How can I do this?

Thanks!


Solution

  • You can simply declare myString as a class member and get the value by a getter method and pass it to your function:

    QString Dialog::getString()
    {
        return myString;
    }
    void MainWindow::on_dnldButton_clicked()
    {
        //Dialog is showed via "Modal approach".
        Dialog newDnld;
        newDnld.setModal(true);
        newDnld.exec();
        dnldFile(newDnld.getString());
    }