Sending QString to Dialog from MainWindow



  • I have a QString in my MainWindow, now when the user click a button a new dialog popup. This dialog has a QLabel which i need to setText with the QString saved in MainWindow! how do i do it?
    Thanks in advance!


  • Moderators

    Either modify the constructor of the dialog to accept additional parameter or add a setter method to your dialog class.



  • ok if go with modifying the constructor, where do i pass the argument?
    in MainWindow.cpp i use the following to launch the dialog
    @Dialog text(this)
    test.exec();@



  • You can modify constructor like this:
    @Dialog ::Dialog (QWidget *parent, QString text) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    ui->label->setText(text);
    }@
    don't forget set default value for text in *.h file
    and
    @Dialog text(this, someText);
    test.exec();@

    or you can add new method for settings text:
    @void Dialog::setLabelText(QString value)
    {
    ui->label->setText(value);
    }@


  • Lifetime Qt Champion

    It should rather be:

    @
    Dialog ::Dialog (QString text, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    ui->label->setText(text);
    }
    @

    to follow Qt's pattern



  • Yes you're right, thanks :)



  • Thanks guys really. I tried the same thing bt i forgot to make the changes in the .h file! thats way it dint work! :D


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.