Meaning of (QWidget *parent = 0) in constructor MyClass(QWidget *parent = 0);
-
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ø.
[quote author="Wilk" date="1337032399"]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":http://qt-project.org/doc/qt-4.8/QObject.html.[/quote] -
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. -
Ok, if You want a top level item You only have to:
@
class Item : public QGraphicsItem
{
public:
Item() {
........
}
};
@Without:
@
Item(QGraphicsItem *parent = 0) : QGraphicsItem(parent) {}
@Right?
-
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.
-
Thanks, you solved my doubt.
[quote author="MuldeR" date="1337039636"]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.[/quote]
-
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 -
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++
-
[quote author="Mariø™" date="1337040692"]Thanks, you solved my doubt.
[quote author="MuldeR" date="1337039636"]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.[/quote]
[/quote]Note that if parent is 0, you need to manually add item to scene ( QGraphicsScene::addItem() ), and of course item will be top level.
-
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:
@int main(int argc, char *argv[])
{QApplication app(argc, argv); MyClass *GUI = new MyClass(); GUI -> show(); return app.exec();
}@
constructor: // called when object GUI is created
@MyClass::MyClass(QWidget *parent) : QWidget(parent)
{
ui.setupUi(this);
...
}@ -
Hi again,
I got now the meaning of the "this ponter" which is just a C++ basic.
See"Meaning of the this pointer":http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr035.htm
So my questions are answered. Thx for the support guys.
-
If I have this code in a QGraphicsRectItem class:
Private:
@
QGraphicsRectItem *item;
@Constructor:
@
item = new QGraphicsRectItem();
@mousePressEvent:
@
this->addItem(item);
@mouseReleaseEvent:
@
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:
@
delete item;
@[quote author="cincirin" date="1337071681"][quote author="Mariø™" date="1337040692"]Thanks, you solved my doubt.
[quote author="MuldeR" date="1337039636"]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.[/quote]
[/quote]Note that if parent is 0, you need to manually add item to scene ( QGraphicsScene::addItem() ), and of course item will be top level.[/quote]