Getting parent dialog variable value in child dialog



  • I have a parent dialog. and i am launching a child dialog from within the parent dialog. code below

    // inside parent dialog
    QString pValue;
    ChildDialog = new ChildDialog(this);
    ChildDialog->exex();

    // inside child dialog
    QString cValue = need to get the value of pValue declared in parent dialog.

    i want to pass the value of a QString value from the parent dialog to child dialog..
    how can i access the QString value in my ChildDialog???
    how can i pass/ access the value from the parent dialog ??

    Thanks.



  • You can pass the value in the constructor:
    @
    class ChildDialog: public QDialog
    {
    QString cValue;
    public:
    ChildDialog(const QString value, QWidget *parent = 0) :
    QDialog(parent), cValue(value)
    {
    // Now cValue == value
    // You can use cValue from other methods of ChildDialog
    }
    }
    @



  • Thanks for the reply.

    do i need to make changes in header as well an in cpp file..

    // in header
    class ChildDialog : public QDialog
    {
    Q_OBJETC
    public:
    QString path;
    explicit ChildDialog(const QString value, QWidget *parent = 0) : QDialog(parent), path(value){}
    ~ChildDialog();

    //in cpp file
    ChildDialg::ChildDialog(QWidget *parent) : QDialog(parent), ui(new Ui::ChildDialog)
    {
    ui->SetupUi(this);
    }

    Showing error, prototype for ChildDialog::ChildDialog(QWidget*) does not match any in class ChildIDialog

    where have i written wrong??



  • Ok.
    I brought all the code in one place for short.
    You can put up the implementation of the constructor in the source file.

    Header ChildDialog (.h or .hpp):
    @
    class ChildDialog: public QDialog
    {
    QString cValue;
    public:
    ChildDialog(const QString value, QWidget *parent = 0);
    void childMethod();
    }
    @

    Source ChildDialog (.cpp):
    @
    ChildDialog::ChildDialog(const QString value, QWidget *parent) :
    QDialog(parent), cValue(value)
    {
    ui->setupUi(this);
    // Use cValue or call other method
    childMethod();
    }
    }
    void ChildDialog::childMethod()
    {
    // Use cValue
    }
    @

    In parent dialog implementation:
    @
    QString pValue = "Hello from parent";
    ChildDialog = new ChildDialog(pValue, this);
    ChildDialog->exec();
    @



  • i changed it..
    // in header
    @
    public:
    QString fPath
    explicit ChildDialog(const QString value, QWidget *parent = 0) ;
    @

    // in source dialog
    @ ChildDialg::ChildDialog(QString value,QWidget *parent) : QDialog(parent),fPath(value) ui(new Ui::ChildDialog)
    {
    }@

    now i am getting the error
    multiple definition of ChildDialog::ChildDialog(QString,QWidget*)
    using the statements below to launch the ChildDialog in my maindialog
    @ QString pValue;
    ChildDialog = new ChildDialog(pValue,this);
    ChildDialog->exex();@

    what went wrong ??

    Thank You



  • Thanks Konstantin
    but why is the error coming for multiple definition ??

    Thanks



  • You are using preprocessor directives in header files?

    Header ChildDialog (.h or .hpp):
    @
    #ifndef CHILD_DIALOG_H
    #define CHILD_DIALOG_H

    // Header code...

    #endif // CHILD_DIALOG_H
    @


Log in to reply
 

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