Results 1 to 10 of 10

Thread: Question about qt objects deletion

  1. #1
    Join Date
    Feb 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Windows

    Exclamation Question about qt objects deletion

    Hello everyone i am a newbie to this form and to qt world and i have a simple question which is how to make my class auto deletes its members by accepting a parent

    Qt Code:
    1. class Myform : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6.  
    7. Myform()
    8. {
    9. b=new QPushButton("OK",this);
    10. l=new QLineEdit(this);
    11. mylabel=new QLabel(this);
    12. }
    13.  
    14. ~Myform()
    15. {
    16.  
    17. }
    18.  
    19. private:
    20.  
    21. QLabel *mylabel;
    22.  
    23. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question about qt objects deletion

    Just like you do in lines 9-11 -- pass the widget pointer as the last argument to every child. Similarily you can modify your constructor to accept a QObject pointer and call the base class implementation where you will pass this pointer to QWidget constructor.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Question about qt objects deletion

    can you please modify the class and show me what do you mean with " pass this pointer to QWidget constructor " and thanks in advance

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Question about qt objects deletion

    Qt Code:
    1. MyForm(QWidget *parent = 0) : QWidget(parent)
    2. {
    3. ...
    4. }
    To copy to clipboard, switch view to plain text mode 

    Btw, you class already deletes its children, this modification is for making instance of your class being deleted by their parents.

    Cheers,
    _

  5. #5
    Join Date
    Feb 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Question about qt objects deletion

    MyForm(QWidget *parent = 0) : QWidget(parent) i sow this expression many times and i didn't get it ....can you explaine please
    and what do you mean by the class already deletes its children
    and last i am sorry about those too many questions i am a confused newbie

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Question about qt objects deletion

    Quote Originally Posted by MI View Post
    MyForm(QWidget *parent = 0) : QWidget(parent) i sow this expression many times and i didn't get it ....can you explaine please
    This is a constructor of a C++ class called MyForm.
    It takes one argument of type QWidget*, named parent, and has a default value of 0.
    The parent argument is passed on to the base class constructor.

    Quote Originally Posted by MI View Post
    and what do you mean by the class already deletes its children
    Because you are passing "this" as the parent of the children. QObject subclasses such as QWidget and thus MyForm delete their children when they are deleted.

    Cheers,
    _

  7. #7
    Join Date
    Feb 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Question about qt objects deletion

    new question : by doing this " MyForm(QWidget *parent = 0) : QWidget(parent) " what are we really saying to the compiler ...
    and please putup with me

  8. #8
    Join Date
    Oct 2013
    Posts
    41
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about qt objects deletion

    Quote Originally Posted by MI View Post
    new question : by doing this " MyForm(QWidget *parent = 0) : QWidget(parent) " what are we really saying to the compiler ...
    and please putup with me
    Whatever widget you create an instance of this form in, when you call "MyForm myForm(this);" You are saying that the widget you called it from is the parent of myForm. When that parent gets deleted, it will call the destructor on all of its children causing a chain reaction.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Question about qt objects deletion

    sulliwk06 already explained the runtime behavior created by this so here the "what are we saying to the compiler" bit:

    we tell the compiler that instance of class MyForm can be constructed with zero or one argument of type QWidget*.

    If no argument is passed to the constructor, then a null pointer should be assumed.

    The given value (either the argument or the null pointer) should be uses as the argument of the invocation of the base class constructor.

    Cheers,
    _

  10. #10
    Join Date
    Feb 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Question about qt objects deletion

    Big thanks to every one who helped me

Similar Threads

  1. QSerialDevice enumerator deletion question
    By marcvanriet in forum Qt Programming
    Replies: 4
    Last Post: 28th December 2011, 02:48
  2. Replies: 3
    Last Post: 9th January 2010, 16:47
  3. Controling the deletion of objects?
    By mooreaa in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2008, 19:08
  4. Replies: 7
    Last Post: 18th July 2006, 22:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.