May 14, 2012

Streight Streight
Lab Rat
26 posts

Meaning of (QWidget *parent = 0) in constructor MyClass(QWidget *parent = 0);

 

hi, just a part of a usual structured class in Qt:

  1. class MyClass  :  public QWidget          
  2. {
  3.  
  4. Q_OBJECT
  5.  
  6.  public:
  7.  
  8.    MyClass(QWidget *parent = 0);
  9. .
  10. .
  11. .
  12. }
  13.  
  14. MyClass::MyClass(QWidget *parent) : QWidget(parent)      
  15. {
  16.     ui.setupUi(this);

Looking at the constructor I don’t understand the meaning of the parameter

  1. (QWidget *parent = 0)
? What does this mean?

Furthermore I would like to know the meaning of (this) in the

  1. ui.setupUi(this)
method?

greetings

 Signature 

I`m Streight ;).

13 replies

May 14, 2012

MuldeR MuldeR
Robot Herder
471 posts

  1. MyClass(QWidget *parent = 0)

This is standard C++ syntax. Parameter with a default value – makes the parameter optional.

The constructor expects a pointer to the parent Widget, so the type of parameter “parent” is QWidget*.

Also the default value is “0”. So if you don’t pass a pointer to the parent Widget into the constructor of MyClass explicitely, then a NULL pointer is passed implicitly (i.e. the new instance of MyClass won’t have a parent).

About ui.setupUi(this) you should refer to the Qt documentation and learn how to use an UI file:
http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html

 Signature 

My OpenSource/Libre software at: http://muldersoft.com/ | Go visit the coop: http://youtu.be/Jay-fG9eaYk

May 14, 2012

Wilk Wilk
Ant Farmer
120 posts

Hello.
Obviously this parameter means that when constructor is called with no parameter then no parent is set for new widget. AFAIK parents are used to create object hierarchies at runtime in order to make it easier to manage objects and to simplify resource management. For more information read documentation about QObject [qt-project.org].

May 15, 2012

Mariø™ Mariø™
Hobby Entomologist
43 posts

Hi,

Does that mean that we need to include the parent parameter for runtime reasons?
Even for classes derived from QGraphicsItem(QGraphicsItem * parent = 0)?

Thx, Mariø.

Wilk wrote:
Hello. Obviously this parameter means that when constructor is called with no parameter then no parent is set for new widget. AFAIK parents are used to create object hierarchies at runtime in order to make it easier to manage objects and to simplify resource management. For more information read documentation about QObject [qt-project.org].
 Signature 

To be something. You need to do something.

May 15, 2012

MuldeR MuldeR
Robot Herder
471 posts

All classes derived from QObject can have a parent:
QObjects organize themselves in object trees. When you create a QObject with another object as parent, the object will automatically add itself to the parent’s children() list. The parent takes ownership of the object; i.e., it will automatically delete its children in its destructor. You can look for an object by name and optionally type using findChild().

For GUI Widgets, QWidget and everything derived from QWidget, the parent has a special meaning:
Widgets without a parent are “top level” (independent) widgets. All other widgets are drawn within their parent.

 Signature 

My OpenSource/Libre software at: http://muldersoft.com/ | Go visit the coop: http://youtu.be/Jay-fG9eaYk

May 15, 2012

Mariø™ Mariø™
Hobby Entomologist
43 posts

Ok, if You want a top level item You only have to:

  1. class Item : public QGraphicsItem
  2. {
  3.     public:
  4.         Item() {
  5.             ........
  6.         }
  7. };

Without:

  1. Item(QGraphicsItem *parent = 0) : QGraphicsItem(parent) {}

Right?

 Signature 

To be something. You need to do something.

May 15, 2012

MuldeR MuldeR
Robot Herder
471 posts

I think you can design the constructor of your “Item” class as you like. If you never want to set a parent for “Item” objects, then you don’t need to have the “parent” parameter in your constructor, of course. You can’t change the constructor of QGraphicsItem, because it’s a pre-defined class from Qt. But, as the definition of QGraphicsItem has a default value for the “parent” parameter, you don’t need to have QGraphicsItem in the initialization list of your Item constructor. The QGraphicsItem’s (default) constructor will simply be called with a NULL parent.

 Signature 

My OpenSource/Libre software at: http://muldersoft.com/ | Go visit the coop: http://youtu.be/Jay-fG9eaYk

May 15, 2012

Mariø™ Mariø™
Hobby Entomologist
43 posts

Thanks, you solved my doubt.

MuldeR wrote:
I think you can design the constructor of your “Item” class as you like. If you never want to set a parent for “Item” objects, then you don’t need to have the “parent” parameter in your constructor, of course. You can’t change the constructor of QGraphicsItem, because it’s a pre-defined class from Qt. But, as the definition of QGraphicsItem has a default value for the “parent” parameter, you don’t need to have QGraphicsItem in initialization list of your Item constructor. The QGraphicsItem constructor will simply be called with a NULL parent.
 Signature 

To be something. You need to do something.

May 15, 2012

Streight Streight
Lab Rat
26 posts

One more question:

Is there a specific technical term for using the setupUi() method? In some way it seems to me that it is like a multi-inheritance since MyClass inherits attributes from QWidget and also from the ui cass object.
greetings

 Signature 

I`m Streight ;).

May 15, 2012

ChrisW67 ChrisW67
Robot Herder
376 posts

Technical term? You are calling a member function on an object. The ui object is being included in your class as a member variable, not by inheritance or member pointer which are the other two variations you can opt for.

Your class gets QWidget behaviours because you inherit from QWidget, not because of anything the the ui member is doing.

None of this is magical or Qt-specific… just plain old C++

May 15, 2012

cincirin cincirin
Ant Farmer
387 posts

Mariø™ wrote:
Thanks, you solved my doubt.

MuldeR wrote:
I think you can design the constructor of your “Item” class as you like. If you never want to set a parent for “Item” objects, then you don’t need to have the “parent” parameter in your constructor, of course. You can’t change the constructor of QGraphicsItem, because it’s a pre-defined class from Qt. But, as the definition of QGraphicsItem has a default value for the “parent” parameter, you don’t need to have QGraphicsItem in initialization list of your Item constructor. The QGraphicsItem constructor will simply be called with a NULL parent.

Note that if parent is 0, you need to manually add item to scene ( QGraphicsScene::addItem() ), and of course item will be top level.

May 15, 2012

Streight Streight
Lab Rat
26 posts

For sure it is a calling the member function setupUi() on the object ui . However, in main function I create an instance of MyClass and show it, but because of the ui.setupUi(this) in the constructor it has the appearance of the object ui . Therefore I thought there maybe was a specific technical term for this.

main function:

  1. int main(int argc, char *argv[])      
  2.  {
  3.  
  4.      QApplication app(argc, argv);  
  5.  
  6.      MyClass *GUI = new MyClass();  
  7.      GUI -> show();                                            
  8.      return app.exec();                                    
  9.  }

constructor: // called when object GUI is created

  1. MyClass::MyClass(QWidget *parent) : QWidget(parent)      
  2. {
  3.     ui.setupUi(this);
  4. ...
  5. }

 Signature 

I`m Streight ;).

May 15, 2012

Streight Streight
Lab Rat
26 posts

Hi again,

I got now the meaning of the “this ponter” which is just a C++ basic.
See

Meaning of the this pointer [publib.boulder.ibm.com]

So my questions are answered. Thx for the support guys.

 Signature 

I`m Streight ;).

May 16, 2012

Mariø™ Mariø™
Hobby Entomologist
43 posts

If I have this code in a QGraphicsRectItem class:

Private:

Constructor:

  1. item = new QGraphicsRectItem();

mousePressEvent:

  1. this->addItem(item);

mouseReleaseEvent:

  1. this->removeItem(item)

Do I must delete the item in the destructor, because the item is not a child of the scene ( this->removeItem(item) )??

Destructor:

  1. delete item;

cincirin wrote:
Mariø™ wrote:
Thanks, you solved my doubt.

MuldeR wrote:
I think you can design the constructor of your “Item” class as you like. If you never want to set a parent for “Item” objects, then you don’t need to have the “parent” parameter in your constructor, of course. You can’t change the constructor of QGraphicsItem, because it’s a pre-defined class from Qt. But, as the definition of QGraphicsItem has a default value for the “parent” parameter, you don’t need to have QGraphicsItem in initialization list of your Item constructor. The QGraphicsItem constructor will simply be called with a NULL parent.

Note that if parent is 0, you need to manually add item to scene ( QGraphicsScene::addItem() ), and of course item will be top level.

 Signature 

To be something. You need to do something.

 
  ‹‹ Help needed : Want to contribute to Qt      qAudioInput -- windows qt 4.7/4.8 - broken over 48000 hz ››

You must log in to post a reply. Not a member yet? Register here!