Pass from parent to child a variable before child's constructor executes?
-
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!
-
Why don't you just pass the text to the Properties constructor?
-
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)) -
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? -
@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
-
Yes, OK, this is what I had in my mind as well :)
Thanks! -
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. -
EDIT: saw new post
-
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.
-
Ok, solved! Thanks again and sorry that I didn't know that a constructor could take more arguments - _ -