Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [SOLVED] QWidget parent question
QtWS25 Last Chance

[SOLVED] QWidget parent question

Scheduled Pinned Locked Moved C++ Gurus
10 Posts 5 Posters 4.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    terietor
    wrote on last edited by
    #1

    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

    terietor.gr

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vsorokin
      wrote on last edited by
      #2

      @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'.

      --
      Vasiliy

      1 Reply Last reply
      0
      • T Offline
        T Offline
        terietor
        wrote on last edited by
        #3

        [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?

        terietor.gr

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          [[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.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vsorokin
            wrote on last edited by
            #5

            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

            --
            Vasiliy

            1 Reply Last reply
            0
            • T Offline
              T Offline
              terietor
              wrote on last edited by
              #6

              [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

              terietor.gr

              1 Reply Last reply
              0
              • L Offline
                L Offline
                luisvaldes88
                wrote on last edited by
                #7

                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.

                Muchos quieren, pocos pueden, algunos consiguen.

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fluca1978
                  wrote on last edited by
                  #8

                  [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...

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    [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.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      luisvaldes88
                      wrote on last edited by
                      #10

                      @
                      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)
                      @

                      Muchos quieren, pocos pueden, algunos consiguen.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved