PDA

View Full Version : What does *parent=0 mean?



veraS
25th June 2010, 20:30
Hello,
When using Qt Creator it does a lot of code thar i will try to understand.
If i create a class based on a QDialog i get this line for the class constructor:

explicit myWidget(QWidget *parent = 0 );
explicit means that i don't want any type cast with my class ... right?
What about the null point to a QWidget?!
What is that for?

Thank you.

tbscope
25th June 2010, 20:54
explicit myWidget(QWidget *parent = 0 );
explicit means that i don't want any type cast with my class ... right?
No, it means you will not be able to do an implicit assignment.
Example:
Suppose you create a class MyInteger that takes an integer in the constructor. Then you can write something like this:
MyInteger integer = 5;
For simple classes, this can work ok, but when you have complex classes, implicit assignments can result in errors. Therefor it is best to have explicit assignments:
MyInteger integer = MyInteger(5);
This way you are sure that everything is correct. It's a safeguard.


What about the null point to a QWidget?!
What is that for?

You define a constructor with a default value. The default value in this case means that there is no parent.
Assigning 0 to a pointer means that you create a pointer to nothing. In any normal case, you want a pointer to point to a certain address in memory, like the address of a parent widget.
But when you don't want to set a parent widget for example, you can point to nothing. Be aware though when using the pointer, always check that it actually points to something (and preferably something correct) or don't do anything with it.

Might I suggest a good C++ book?

veraS
26th June 2010, 13:06
Might I suggest a good C++ book?

Yes:)

What would that be?

tbscope
26th June 2010, 13:22
Sure:
http://freecomputerbooks.com/langCppBooks.html

I can't recommend one in particular though.

veraS
26th June 2010, 13:29
Imagine you have money enougth to buy a single book :rolleyes:
One that could provide you with a vision of the ALL THING ... without beeing specialised in a particular area.

Zlatomir
26th June 2010, 14:20
Imagine you have money enougth to buy a single book :rolleyes:
One that could provide you with a vision of the ALL THING ... without beeing specialised in a particular area.
Such a book hasn't been written (at least not yet, and probably won't ever be written ;) )

For beginners i saw a lot of people recommending Thinking in C++ (http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html) written by Bruce Eckel, i didn't read them but they must be good (there are two volumes, both are free e-books)

For some performance related topics you can use Effective and More Effective C++ written by Scott Meyers (but from these you will learn some tricks to write simple, easy to read and off-course fast code)
And C++ programming language written by Bjarne Stroustrup (i don't recommend this for beginners, but it's a must have in every programmer's library)

For Qt i really recommend: Foundations of Qt Development written by Johan Thelin. I'm currently learning Qt from this one, and it's a very good book.
(but read at least one C++ book before, you will find Qt very simple, intuitive, easy to learn and use)

And last thing: Don't expect a book to teach you everything (you will need a lot of books just to "scratch the surface" of programming using C++ and Qt)

veraS
27th June 2010, 13:12
And C++ programming language written by Bjarne Stroustrup (i don't recommend this for beginners, but it's a must have in every programmer's library)

Just before i start reading this ... one more question.


myWidget::myWidget(QWidget *parent) :
QDialog(parent),
ui(new Ui::myWidget)

Let me make some afirmations here:
1
The Class constructor accept one parameter, that is a pointer to a QWidget

2
The constructor initialization list has two items: QDialog(parent), ui(new Ui::myWidget)

3
ui(new Ui::myWidget) -> This creates an object ui from class myWidget defined in the Ui namespace(the ui file created in Designer that inherits the Ui_myWidget class)

Is this OK?

What is QDialog(parent) doing here?

Please be patient :o

tbscope
27th June 2010, 14:45
1
The Class constructor accept one parameter, that is a pointer to a QWidget
That is correct


2
The constructor initialization list has two items: QDialog(parent), ui(new Ui::myWidget)
That is correct


3
ui(new Ui::myWidget) -> This creates an object ui from class myWidget defined in the Ui namespace(the ui file created in Designer that inherits the Ui_myWidget class)

Is this OK?
Yes, that is ok.


What is QDialog(parent) doing here?
Ahh, you say that myWidget needs to be based on QDialog (you have created a QDialog subclass). The constructor of QDialog also accepts a pointer to a widget, the parent of the dialog (usually a main window).
If you do not pass the widget you set in your subclass (myWidget) to the QDialog class, the QDialog doesn't know or have a parent. Thus, usually you just copy the parameters of the class you base your new class on and pass them to the underlying class. You can, if you want, add other parameters to the constructor of myWidget too.

What you do with QDialog(parent) is telling that your myWidget is based on QDialog and you set the parent via QDialog (as the QDialog contains some code to handle the parent).

Suppose you have this:

class MySuperWidget: public widget1, public widget2
{
public:
MySuperWidget(SomeThingForWidget1 *item1, SomeThingForWidget2 *item2);
...

private:
int myInt1;
int myInt2;
};

Then you write the initilisation like this:



MySuperWidget::MySuperWidget(SomeThingForWidget1 *item1, SomeThingForWidget2 *item2)
: Widget1(item1),
Widget2(item2),
myInt1(5),
myInt2(10)
{
....
}

The initialisation is ordered. First the classes you based your subclass on (in order) and QObject is always first.
Then your member variables, in order of defining them. You can leave one out, but the order remains.


Please be patient :o[/QUOTE]

Zlatomir
27th June 2010, 17:57
A little addition to what tbscope already said, about this question:


...
2
The constructor initialization list has two items: QDialog(parent), ui(new Ui::myWidget)
...

QDialog(parent) // is passing arguments to the QDialog constructor, your class derives from QDialog, (QDialog constructor is called when you create your derived class object) you pass the parent and in some other situation you might need to initialize some other things (from Base class) with this method.
and the second is just initialization

veraS
27th June 2010, 20:32
ahh ... i see!
Thanks