[SOLVED] QWidget parent question



  • Hello,

    i don't know who to describe it,so let me show you

    @

    class MyClass : public QTreeView
    {
    MyClass::MyClass(QWidget parent = 0); // i understand this one
    };

    MyClass::MyClass(QWidget parent = 0)
    :QTreeView(parent)//i don't understand the usage of this line.
    {
    ....
    }
    @

    thanks in advance



  • @MyClass::MyClass(QWidget parent = 0);@

    It is default parameterm i.e if user call constructor witout params, parameter 'parent' will be set to '0'

    @:QTreeView(parent)@

    It mean that need call base class constructor with parameter 'parent'.



  • [quote author="Vass" date="1317044432"]@MyClass::MyClass(QWidget parent = 0);@
    @:QTreeView(parent)@
    It mean that need call base class constructor with parameter 'parent'.
    [/quote]

    if i don't use it.nothing changes in my application.
    What will happen if i don't use it?



  • [[Doc:QObject]] parent child relationship is not setup properly and you may run into memory leaks an other problems.

    Also, in the class definition you are not allowed to add a default value for a parameter:

    So instead of this

    @
    MyClass::MyClass(QWidget parent = 0)
    :QTreeView(parent)//i don't understand the usage of this line.
    {
    ....
    }
    @

    write this:

    @
    MyClass::MyClass(QWidget parent)
    :QTreeView(parent)//i don't understand the usage of this line.
    {
    ....
    }
    @

    And the line you marked as not understood: You call the base class constructor with the parent. If you do not know what this (= base class constructor call) is, you should read some C++ introduction very soon, as this is very basic knowledge about the programming language and is by no means specific to Qt and you will run into serious problems otherwise.



  • If you doing own default constructor without parameter in this case, your class lose auto destruction ability when parent widget was destructed. and you will need always delete your widget with 'delete' command otherwise you will have memory leaks



  • [quote author="Volker" date="1317046382"][[Doc:QObject]] parent child relationship is not setup properly and you may run into memory leaks an other problems.[/quote]

    this one solves my question.

    [quote]
    Also, in the class definition you are not allowed to add a default value for a parameter:
    [/quote]

    correct

    thank you for your help



  • I am not 100% sure but as far I am concerned, QObject has a list of its children, and every time you call a constructor like:

    @
    MyClass::MyClass(QWidget parent = 0)
    :QTreeView(parent)
    @

    you add MyClass as a child of the QObject object pointed by the parent pointer, so when the "parent's"
    destructor is called, it destroys all its children.

    That is why you don't need to worrie about memory leaks when your classes inherits QObject.



  • [quote author="luisvaldes88" date="1317084021"]
    That is why you don't need to worrie about memory leaks when your classes inherits QObject.[/quote]

    And you use the parenting right...



  • [quote author="luisvaldes88" date="1317084021"]I am not 100% sure but as far I am concerned, QObject has a list of its children, and every time you call a constructor like:

    @
    MyClass::MyClass(QWidget parent = 0)
    :QTreeView(parent)
    @

    you add MyClass as a child of the QObject object pointed by the parent pointer, so when the "parent's"
    destructor is called, it destroys all its children.

    That is why you don't need to worrie about memory leaks when your classes inherits QObject.[/quote]

    If you add this constructor (which will not compile, BTW, due to the default argument in the definition = .cpp file), it will just provide a "Qt compatible" constructor. Whether the object is parented or not depends on how you call the constructor in your client code:

    @
    MyClass *obj1 = new MyClass(this); // parented
    MyClass *obj2 = new MyClass; // NOT parented
    @

    If you do not add the QWidget *parent parameter, you cannot create a parented object with new, but must call setParent() (which is cumbersome and not Qt-ish).

    In general it is a good idea to mimic all the constructors of your base class.



  • @
    MyClass::MyClass(QWidget parent = 0)
    :QTreeView(parent)
    @

    sorry about this, I just copy paste the same code on the hurry, I did not compile it mentally before posting... :D

    this one compiles

    @
    MyClass::MyClass(QWidget parent)
    :QTreeView(parent)
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.