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. Pass from parent to child a variable before child's constructor executes?
Forum Updated to NodeBB v4.3 + New Features

Pass from parent to child a variable before child's constructor executes?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 6.1k 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.
  • A Offline
    A Offline
    alexandros
    wrote on last edited by
    #1

    Hello.
    As the title says, I need to pass a variable from the mainwindow to a dialog, that the dialog needs to its constructor, that's why I need firstly to pass the variable and then execute the constructor of the dialog so as to work properly.
    The parent's code:
    @Properties = new properties(this);
    connect(this,SIGNAL(filename(QString)),Properties,SLOT(filename_sent(QString))); //<--making the connection...
    Properties->show(); // <-- this executes the constructor
    emit filename(ui->listWidget->currentItem()->text()); //<--this passes the variable, too late, the constructor has already been executed!@

    thx in advance for any replies!

    1 Reply Last reply
    0
    • H Offline
      H Offline
      HuXiKa
      wrote on last edited by
      #2

      Why don't you just pass the text to the Properties constructor?

      If you can find faults of spelling in the text above, you can keep them.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cincirin
        wrote on last edited by
        #3

        just change your Properties constructor with Properties(QWidget* parent, QString& filename)
        (or if your Properties class don't change the filename, the constructor should be Properties(QWidget* parent, const QString& filename))

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alexandros
          wrote on last edited by
          #4

          Thanks for your replies.
          Interesting, as I am quite a newbie, I'd never thought that a constructor can take itself arguments.
          Well, and if I change it to Properties(QWidget* parent, const QString& filename)), then how would I pass the variable actually :/ ? I thought:
          @Properties = new properties(this, QString);@
          but not sure....is this right?

          1 Reply Last reply
          0
          • H Offline
            H Offline
            HuXiKa
            wrote on last edited by
            #5

            @Properties = new Properties(ui->listWidget->currentItem()->text(), this}
            @

            In the properties.h file
            @...
            public:
            Properties(const QString& filename, QWidget* parent = 0) :
            m_FileName(filename){} // or whatever your filename attribute is, in this exampe it's m_FileName
            ...}@

            You can pass any kind of arguments to a constructor (just like for functions), just don't forget to add it to your .h file.

            The longer version of the above code is, but it does just the same (use the above one, it's a better practice if you don't do anything in your constructor beside setting the attributes):

            @...
            public:
            Properties(const QString& filename, QWidget* parent = 0){ m_FileName = filename; }
            ...}@

            edit: my fingers slipped, thanks Andre

            If you can find faults of spelling in the text above, you can keep them.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              alexandros
              wrote on last edited by
              #6

              Yes, OK, this is what I had in my mind as well :)
              Thanks!

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                First of all, the Qt API guide tells you to put the parent argument last. So instead of
                @
                Properties(QWidget* parent, QString& filename)
                @
                I'd suggest you use:
                @
                Properties(QString& filename, QWidget* parent = 0)
                @

                This forum is not really about learning the basics of C++. A constructor is a function that can take arguments, and those arguments are used a lot in Qt. All QObject derived classes have a constructor that take at least one...

                You would call it with an actual value though, not with a type name:
                @
                Properties* p = new Properties(QString("My Needed String"), this);
                //or:
                QString s = QString("My Needed String");
                Properties* p = new Properties(s, this);
                @
                Though it is wise to use more descriptive names for your variables than just p and s, with i being the semi-exception for loop counters.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  alexandros
                  wrote on last edited by
                  #8

                  EDIT: saw new post

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    Edit: This was a reply to the now removed message above.

                    Sorry, this is basic C++, and the error message is quite clear: parameters with default values must be the last parameters in a parameter list, thus either:

                    • Remove the default value from your first parameter, or
                    • Add a default value to your second parameter, or
                    • Switch the parameter order so that the parameter with the default value is the last parameter.

                    The latter is most in line with how Qt does it, but all methods fix your issue.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      alexandros
                      wrote on last edited by
                      #10

                      Ok, solved! Thanks again and sorry that I didn't know that a constructor could take more arguments - _ -

                      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