PDA

View Full Version : pass parent parameter to base class constructor



phil_n
5th April 2014, 16:29
Hi all,

I'm studying this book "C++ GUI programming with Qt4" trying to learn it but my C++ was always rudamentary and even that is very rusty. I'm trying to make sure I understand it rather than just copy out examples. So in one of the examples there is this construct:


FindDialog::FindDialog(QWidget *parent) : QDialog(parent)

wich is described in the text this way:


we pass on the parent parameter to the base class constructor

then it's instantiated like this:


FindDialog *dialog = new FindDialog;

ie. with no parameters.

Can anybody explain what is going on here? Or where I can read up on it (in newbie speak)?
I know it's sub-classing QDialog but what does 'parent' refer to? Where does it come from?

Thanks.

Lesiok
5th April 2014, 17:34
Something is a declaration :
FindDialog::FindDialog(QWidget *parent = NULL);
I.e. parameter have a default value. You should first get to know C++.

phil_n
5th April 2014, 18:13
Thanks. I took some programming programs at night school years ago (BASIC, TurboPascal, C++, even a separate one for pointers) but never used it and, obviously, forgot most of it. Never even considered the default value - duh. So if I wanted to instantiate but with a parent I would do something like:


FindDialog *dialog = new FindDialog(<pointer to parent widget>);

And, because this widget is to be the 'window' I can basically ignore the FindDialog parameter (so it gets the default of no parent)?

anda_skoa
7th April 2014, 09:43
In most cases you could do something like this



FindDialog *dialog = new FindDialog(this);


I.e. in most cases when you create a dialog you are inside the code of a widget.

Cheers,
_