Results 1 to 6 of 6

Thread: delete layout() / setLayout issue

  1. #1
    Join Date
    Sep 2007
    Posts
    7
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default delete layout() / setLayout issue

    hello everyone,
    i have a little problem and i cand't find the problem.
    (all code it's in the constructor of a class that extends QWidget)

    mainLayoutBigg = new QGridLayout((QWidget*)this);
    mainLayoutSmall = new QGridLayout((QWidget*)this);

    mainLayoutBigg->addWidget(qwd_1,...);
    mainLayoutBigg->addWidget(qwd_2,...);
    mainLayoutBigg->addWidget(qwd_3,...);

    mainLayoutSmall->addWidget(qwd_1,...);
    mainLayoutSmall->addWidget(qwd_2,...);

    this->setLayout(mainLayoutSmall);
    delete layout();
    this->setLayout(mainLayoutBigg); [COLOR="Red"]<=== here dies ..., more exactly in a QWidget.cpp setParent(this) ---> QObjectPrivate::setParent_helper(QObject *o), parentD->children[index] = 0; because index is -1[/COLOR]

    what i'm trying to do is to swich between 2 layouts ... ibut i don;t know that's wrong with the code above. if anyone cand help ...

    ps: Qt 4.3.1

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: delete layout() / setLayout issue

    You better use a QStackedWidget.

    I think the problem is that you add widgets to both layouts, and you delete one of them. You should add the widgets to the second layout after the first one is deleted.
    This code:
    Qt Code:
    1. mainLayoutBigg = new QGridLayout((QWidget*)this);
    2. mainLayoutSmall = new QGridLayout((QWidget*)this);
    To copy to clipboard, switch view to plain text mode 
    Creating the layouts with parents causes them to be set just as using setLayout.
    Last edited by marcel; 21st September 2007 at 13:43.

  3. #3
    Join Date
    Sep 2007
    Posts
    7
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: delete layout() / setLayout issue

    mainLayoutSmall=041315C8 mainLayoutBigg=04131448,

    after setLayout(mainLayoutSmall), layout()=041315C8
    mainLayoutSmall=041315C8 mainLayoutBigg=04131448,
    after delete layout, layout()=00000000

    mainLayoutSmall=041315C8 mainLayoutBigg=04131448,
    after setLayout, layout(mainLayoutBigg)=04131448
    mainLayoutSmall=041315C8 mainLayoutBigg=04131448,
    after delete layout, layout()=00000000

    so ... delete layout() deletes mainLayoutSmall and mainLayoutBigg objects but mainLayoutSmall and mainLayoutBigg pointers still pointing some adress ....

    ok, how cand i switch between two layouts then ? i whant to create in constructir 2 layouts and switch between them later in the app ?

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: delete layout() / setLayout issue

    Don't delete the layouts then. Just set them.
    This way your objects will remain intact.
    You don't have any reason to delete the layout because QWidget does not create a copy when you set it. It uses the one you pass.

    so ... delete layout() deletes mainLayoutSmall and mainLayoutBigg objects but mainLayoutSmall and mainLayoutBigg pointers still pointing some adress ....
    Of course they are. Nobody makes them null.

  5. #5
    Join Date
    Sep 2007
    Posts
    7
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: delete layout() / setLayout issue

    not so easy.... Acording to http://doc.trolltech.com/4.3/qwidget.html#setLayout, You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: delete layout() / setLayout issue

    Just implement the alternating part of the layout as QStackedWidget (or QStackedLayout) and the problem is solved. This will also save you from possible upcoming flicker in case you'll get the layout switch hack working..
    J-P Nurmi

Similar Threads

  1. Qt layout memory issue
    By bunjee in forum Qt Programming
    Replies: 9
    Last Post: 25th August 2007, 17:11
  2. Qt layout issue
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 15th August 2007, 19:43
  3. c++, placement delete upon exception
    By stinos in forum General Programming
    Replies: 6
    Last Post: 31st October 2006, 15:38

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.