Results 1 to 3 of 3

Thread: destroing nested layout

  1. #1
    Join Date
    May 2009
    Posts
    75
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default destroing nested layout

    Suppose I have the following code

    MainWindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QDialog>
    5.  
    6. class MainWindow : public QDialog
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MainWindow(QWidget *parent = 0);
    12. ~MainWindow();
    13. };
    14.  
    15. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    MainWindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QHBoxLayout>
    3. #include <QVBoxLayout>
    4. #include <QLabel>
    5.  
    6. MainWindow::MainWindow(QWidget *parent)
    7. : QDialog(parent)
    8. {
    9. QHBoxLayout *mainLayout = new QHBoxLayout(this);
    10. QHBoxLayout *slaveLayout = new QVBoxLayout(this);
    11.  
    12. slaveLayout->addWidget(new QLabel("A0"));
    13. slaveLayout->addWidget(new QLabel("A1"));
    14.  
    15. mainLayout->addLayout(slaveLayout);
    16. mainLayout->addWidget(new QLabel("B0"));
    17.  
    18. setLayout(mainLayout);
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. delete layout();
    24. }
    To copy to clipboard, switch view to plain text mode 

    does the destrictor delete both mainLayout and slaveLayout?
    Or do I need to modify as follows:

    Qt Code:
    1. MainWindow::~MainWindow()
    2. {
    3. delete layout()->layout();
    4. delete layout();
    5. }
    To copy to clipboard, switch view to plain text mode 

    thanks
    Last edited by mastupristi; 26th November 2009 at 12:45. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: destroing nested layout

    Quote Originally Posted by mastupristi View Post
    does the destrictor delete both mainLayout and slaveLayout?
    Yes. You have to do nothing.

  3. #3
    Join Date
    May 2009
    Posts
    75
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: destroing nested layout

    Quote Originally Posted by Lykurg View Post
    Yes. You have to do nothing.
    Ok, that's fine

    Now I wonder if widgets are deleted too.

    If I have the following contructor:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QHBoxLayout>
    3. #include <QVBoxLayout>
    4. #include <QLabel>
    5.  
    6. MainWindow::MainWindow(QWidget *parent)
    7. : QDialog(parent)
    8. {
    9. QHBoxLayout *mainLayout = new QHBoxLayout(this);
    10. QHBoxLayout *slaveLayout = new QVBoxLayout(this);
    11.  
    12. static QLabel A0("A0");
    13. slaveLayout->addWidget(&A0);
    14. slaveLayout->addWidget(new QLabel("A1"));
    15.  
    16. mainLayout->addLayout(slaveLayout);
    17. mainLayout->addWidget(new QLabel("B0"));
    18.  
    19. setLayout(mainLayout);
    20. }
    To copy to clipboard, switch view to plain text mode 

    and desctructor:
    Qt Code:
    1. MainWindow::~MainWindow()
    2. {
    3. delete layout();
    4. }
    To copy to clipboard, switch view to plain text mode 

    does the destructor destroy also QLabel A0, "A1" and "B0"? I wonder if "A1" and "B0" are deallocated, and what about A0 that is statically allocated

    thanks

Similar Threads

  1. changing layout of a widget
    By mikro in forum Qt Programming
    Replies: 10
    Last Post: 4th August 2009, 20:21
  2. Qt like Layout Manager available for .NET platform
    By vkhaitan in forum Qt Programming
    Replies: 0
    Last Post: 5th November 2008, 13:36
  3. Nested Layout
    By starcontrol in forum Qt Programming
    Replies: 2
    Last Post: 9th April 2008, 12:47
  4. Qt layout memory issue
    By bunjee in forum Qt Programming
    Replies: 9
    Last Post: 25th August 2007, 17:11
  5. Qt4 widget and nested layout issue.
    By bunjee in forum Qt Programming
    Replies: 12
    Last Post: 18th January 2007, 20:29

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.