Results 1 to 4 of 4

Thread: Allocating widgets on the stack verses using the heap

  1. #1
    Join Date
    May 2010
    Posts
    24
    Thanks
    14
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Allocating widgets on the stack verses using the heap

    I am just curious if there are any gotcha's to creating widgets on the stack? In the QT examples I've looked at, typically the main function looks like this:

    int main(int argc, char** argv)
    {
    QApplication app(argc, argv);
    MainWindow mw(0, "mainwindow");

    if (mw.Initialize())
    {
    app.setMainWidget(&mw);
    mw.show();
    return app.exec();
    }

    return 0;
    }


    Here, the instance of my MainWindow is created on the stack. However, the widgets that make up the MainWindow are all allocated on the heap using new. A typical header looks something like this:

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow(QWidget* pParent=0, const char* pInternalName=0, WFlags Flags=WType_TopLevel);
    ~MainWindow();

    private:
    QMenuBar* mp_MenuBar;
    QWidget* mp_CentralWidget;
    QVGroupBox* mp_ControlPane;
    StatusPane* mp_StatusPane;
    ...


    You can see that pointers to widgets are used in the header -- but why? If the MainWindow widget is going to create a static display, why not declare them as actuals objects and simply construct them in the initializer list for the MainWindow constructor?

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Allocating widgets on the stack verses using the heap

    Its fine to create objects on the stack as long as you know that the lifetime of the object will be less than the lifetime of that particular stack frame.

    For example, the main call is not terminated until app.exec returns, therefore it is safe to create objects on the stack here.

    This is not always the case, so for automated tools such as QtDesigner/UIC, it is safer to always create them on the heap.

  3. The following user says thank you to squidge for this useful post:

    Ronayn (5th August 2011)

  4. #3
    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: Allocating widgets on the stack verses using the heap

    And a general rule of a thumb is to always create objects with a parent on the heap and not on the stack.
    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.


  5. The following user says thank you to wysota for this useful post:

    Ronayn (5th August 2011)

  6. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Allocating widgets on the stack verses using the heap

    You might be aware of the fact creating on the stack will limit the scope of the object to the block in with it is declared, and objects on heap will have a global scope.

    There is one more use if object is declared in heap (I mean as a pointer), it is dynamic is nature, now here dynamic here refers not only to the memory being allocated but also to the object itself, i.e. the object it self can be changed dynamically with another object / derived custom object.

    If you see at the example of QMainWindow (as you pointed out), even through is created on stack, it internally has pointers, such that mainwindow internal widgets can be are created on heap. Now here the internal objects could also have been create on the stack as member objects, but that it is design / requirement of QMainWindow.

    As an example, it is required for the QMainWindow to have an ability to change the centralWidget at runtime, so to support the feature of changing the centralWidget, mp_CentralWidget pointer is used. Well you can still use the mp_CentralWidget pointer of QMainWindow to point to a widget created on the stack.

    Try this, create a QMainWindow on stack, and also any other widget on stack, and set this widget as the centralWidget of the MainWindow, the internal pointer of the mainwindow will now point to the widget which is on stack.

    Quote Originally Posted by Ronayn
    You can see that pointers to widgets are used in the header -- but why? If the MainWindow widget is going to create a static display, why not declare them as actuals objects and simply construct them in the initializer list for the MainWindow constructor?
    As I explained above, pointers to widgets are used to support the ability to change the widget dynamically, if created as member objects, they cannot to replaced with user defined widgets.

    So to conclude, I would say, pointers are used internally in QMainWinodow in order to give dynamic features, but not as an intention to create widgets on heap.

  7. The following user says thank you to Santosh Reddy for this useful post:

    Ronayn (5th August 2011)

Similar Threads

  1. Replies: 3
    Last Post: 12th February 2011, 13:07
  2. Replies: 5
    Last Post: 29th November 2009, 15:09
  3. Problems with allocating QSqlTableModel on heap
    By kosa in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2009, 17:56
  4. 2 questions: QAuthenticator and stack / heap
    By Tito Serenti in forum Qt Programming
    Replies: 3
    Last Post: 3rd March 2009, 06:56
  5. stack, heap and C#
    By mickey in forum General Programming
    Replies: 8
    Last Post: 20th August 2007, 18:40

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.