Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting parent dialog variable value in child dialog
Forum Updated to NodeBB v4.3 + New Features

Getting parent dialog variable value in child dialog

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 4.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    subrat17june
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • podsvirovP Offline
      podsvirovP Offline
      podsvirov
      wrote on last edited by
      #2

      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
      }
      }
      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        subrat17june
        wrote on last edited by
        #3

        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??

        1 Reply Last reply
        0
        • podsvirovP Offline
          podsvirovP Offline
          podsvirov
          wrote on last edited by
          #4

          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();
          @

          1 Reply Last reply
          0
          • S Offline
            S Offline
            subrat17june
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              subrat17june
              wrote on last edited by
              #6

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

              Thanks

              1 Reply Last reply
              0
              • podsvirovP Offline
                podsvirovP Offline
                podsvirov
                wrote on last edited by
                #7

                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
                @

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved