Results 1 to 19 of 19

Thread: Remove a QLayout from QGridLayout

  1. #1
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Remove a QLayout from QGridLayout

    Happy new Year

    I have a QGridLayout and added some QLayouts, which I want to remove now:

    Qt Code:
    1. leftBox->addLayout(itemList[itemID]);
    2. leftBox->removeItem(itemList[itemID]);
    3. itemList[itemID]->setParent(0);
    To copy to clipboard, switch view to plain text mode 

    leftBox is my QGridLayout
    itemList is a QList which contains Points of QHBoxLayout. The adding of the layouts is no problem.
    In this example I want to remove them immediatly. removeItems() puts the removed layouts to the same place (left top corner of the leftBox). I actually thought this is as the added Layouts have still leftBox as parent. So I set another parent, which was useless.

    So how can I remove this layouts without deleting the removed layout as I want to add it later again?

  2. #2
    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: Remove a QLayout from QGridLayout

    You would have to hide the removed widgets. Removing a widget from a layout only means that the widget is no more managed by the layout and so it will stay in the upper left corner unless it is re-parented. I do personally find it a good idea to avoid dynamically changing layouts as much as possible and use for example a stacked widget or layout instead. Of course they don't fit to all situations. In this case you would most likely have to disable the layout during the changes to avoid the flicker, at least if there are several changes in a row.
    J-P Nurmi

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

    grosem (1st January 2007)

  4. #3
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Remove a QLayout from QGridLayout

    Stacked Layout is no alternative for me, but thanks for your advise, it worked.

    EDIT: But I got this problem:

    Qt Code:
    1. QLayout::addChildLayout: layout "" already has a parent
    To copy to clipboard, switch view to plain text mode 

    I did

    Qt Code:
    1. topBox->removeItem(itemList[itemID]);
    2. itemList[itemID]->hide();
    To copy to clipboard, switch view to plain text mode 

    After this I tried to add it at another Layout.

    I thought by removing the item (which is a layout) I can add it somewhere else. But it seems it still keeps its parent!?
    Last edited by grosem; 1st January 2007 at 12:26.

  5. #4
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Remove a QLayout from QGridLayout

    The only possibility I found is to make a new Item with the same value and delete the old one. But this can't be the right solution, can it?

  6. #5
    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: Remove a QLayout from QGridLayout

    QLayout::addChildLayout: layout "" already has a parent
    Sounds like you are missing the
    Qt Code:
    1. itemList[itemID]->setParent(0);
    To copy to clipboard, switch view to plain text mode 
    which you had earlier..?

    Could you describe the GUI you are building? Maybe there's another way for constructing something similar?
    J-P Nurmi

  7. #6
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Remove a QLayout from QGridLayout

    Quote Originally Posted by jpn View Post
    Sounds like you are missing the
    Qt Code:
    1. itemList[itemID]->setParent(0);
    To copy to clipboard, switch view to plain text mode 
    which you had earlier..?

    Could you describe the GUI you are building? Maybe there's another way for constructing something similar?

    I tried this too. But got a

    X Error: BadWindow (invalid Window parameter) 3
    Major opcode: 25 (X_SendEvent)
    Resource id: 0x20000ad
    Well, I have three frames, which have Items. These items are a QHBoxLayout which has 2 QLabels and a QSpinBox. The user should be able to drag these items between the three frames.

    At first I tried the QItemView approach, but I wasn't able to do it the way I wanted.

    I know my new approach using Layouts isn't the right one, but it is much simplier for me. Just the thing of removing does not work yet.

    Thanks for your help

  8. #7
    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: Remove a QLayout from QGridLayout

    Yeah, the suggestion would have been using item views..
    J-P Nurmi

  9. #8
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Remove a QLayout from QGridLayout

    I never thought this reuse of Layouts would be a problem

  10. #9
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Remove a QLayout from QGridLayout

    I don't get it:

    Qt Code:
    1. leftBox->addLayout(protoItem2);
    2. leftBox->removeItem(protoItem2);
    3. protoItem2=0;
    4. delete protoItem2;
    To copy to clipboard, switch view to plain text mode 

    Why is there still the layout in the leftBox??? Even more weird is that the added layout had two QLabels, but only one is still visible (the one with a QPixmap). leftBox->update() changes nothing.

  11. #10
    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: Remove a QLayout from QGridLayout

    Qt Code:
    1. leftBox->addLayout(protoItem2);
    2. leftBox->removeItem(protoItem2);
    3. protoItem2=0;
    4. delete protoItem2; // <--- same as "delete 0;"
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. leftBox->addLayout(protoItem2);
    2. leftBox->removeItem(protoItem2);
    3. delete protoItem2; // delete first
    4. protoItem2=0; // then set the pointer to 0
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  12. #11
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Remove a QLayout from QGridLayout

    ok, that's of course true, but unfortunatly that does not change something.

    I did following: The Items I add to the QGridLayout are from a class derived from QHBoxLayout. I added two QLabels to this class and added them in the Constructor to the QHBoxLayout. I actually though by deleting the object of the class the two QLabels would be deleted too. But apparently that's not the case. Having added this in the destructor

    Qt Code:
    1. ~Item(){delete myName; delete myIcon;}
    To copy to clipboard, switch view to plain text mode 

    it works.

    Seems very dirty for me

  13. #12
    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: Remove a QLayout from QGridLayout

    Those widgets are not children of the layout. Widgets added to a layout become children of the widget where the layout is installed on. This is why the widgets didn't get deleted still if the layout was deleted.
    J-P Nurmi

  14. The following user says thank you to jpn for this useful post:

    grosem (1st January 2007)

  15. #13
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Remove a QLayout from QGridLayout

    Quote Originally Posted by jpn View Post
    Those widgets are not children of the layout. Widgets added to a layout become children of the widget where the layout is installed on. This is why the widgets didn't get deleted still if the layout was deleted.
    Aaah... I was not aware of this. Thank you

  16. #14
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Remove a QLayout from QGridLayout

    Ok, now the removing of QLayouts works. Unfortunatly I can't add new QLayouts to the QGridLayout at runtime. If I want to add them in the constructor of my QMainWindow it's no problem. But adding them later does not work properly. So the QGridLayout receives the new Layout but does not display it.
    I read the Qt-list archive, where someone suggested to call activate on the QGridLayout, but this does not work - not a surprise as this suggestion is from 1998...

    Any ideas?

    EDIT: update() does not change anything
    Last edited by grosem; 2nd January 2007 at 09:15.

  17. #15
    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: Remove a QLayout from QGridLayout

    It should work. Did you, by any chance, forget to show() the widgets added at runtime?
    J-P Nurmi

  18. #16
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Remove a QLayout from QGridLayout

    Quote Originally Posted by jpn View Post
    It should work. Did you, by any chance, forget to show() the widgets added at runtime?
    I do the same as I did in the constructor of QMainWindow (where the Items are shown). If I did not have to call show() there explicitly I don't have to do it somewhere else, have I?

    The Items I add are looking like that


    QHBoxLayout->QFrame->QLabel and QLabel;

    If I call show on QFrame it opens seperatly new windows. show() at the QLabels has no effect.

  19. #17
    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: Remove a QLayout from QGridLayout

    Quote Originally Posted by grosem View Post
    I do the same as I did in the constructor of QMainWindow (where the Items are shown). If I did not have to call show() there explicitly I don't have to do it somewhere else, have I?
    Child widgets (which are not explicitly hidden) become visible too when their ancestor is shown. So you don't have to explicitly call show() for child widgets created in window constructor or so, as you will call show() for the window later anyway. But later at runtime when the window is already visible, adding more child widgets requires also an explicit show() to be called on the children.

    Quote Originally Posted by grosem View Post
    If I call show on QFrame it opens seperatly new windows. show() at the QLabels has no effect.
    Sounds like a parenting issue. A widget with no parent is same as a top level window. So the QFrame has no parent. And the QLabel won't be visible if it's parent (QFrame) is hidden.

    Double check the code where you do the reparenting/layout switching. There must be a quirk somewhere.
    J-P Nurmi

  20. #18
    Join Date
    Jul 2006
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Remove a QLayout from QGridLayout

    EDIT: Maybe I found the problem
    EDIT2: Indeed I did. Ok, I did not, but it works now . As you said I had to call show() to the single widgets.







    Quote Originally Posted by jpn View Post
    Sounds like a parenting issue. A widget with no parent is same as a top level window. So the QFrame has no parent. And the QLabel won't be visible if it's parent (QFrame) is hidden.

    Double check the code where you do the reparenting/layout switching. There must be a quirk somewhere.
    There was a small mistake: I did it this way

    QHBoxLayout->QFrame->QHBoxLayout->QLabel and QLabel;

    The additional QFrame is for easy layouting and dragndrop.

    So I add the QFrame to the QHBoxLayout. Is not the QHBoxLayout now automatically the new parent? Because of your suggestion I tried

    Qt Code:
    1. addWidget(itemframe) //it's in the constructor of the derived QHBoxLayout class
    2. itemframe->setParent(this) //this is the QHBoxLayout
    To copy to clipboard, switch view to plain text mode 

    but the compiler says that only a QWidget can be the parent of itemframe :-/

    btw.: jpn, you're my personal hero for this month
    Last edited by grosem; 2nd January 2007 at 10:29.

  21. #19
    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: Remove a QLayout from QGridLayout

    Glad to hear it finally works.

    By the way, the usual approach to manage child widgets is to subclass some widget and do the handling there. It is quite unusual to subclass a layout for handling children, at least if no changes to the actual layout handling is made.
    J-P Nurmi

Similar Threads

  1. Replies: 6
    Last Post: 13th February 2014, 12:46
  2. QRegExp Help; remove all html tag
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 27th July 2006, 13:40
  3. QGridLayout
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 29th June 2006, 20:34
  4. QPixmap and QGridLayout
    By Talon_Karrde in forum Qt Programming
    Replies: 5
    Last Post: 22nd February 2006, 12:27
  5. QSettings again ... how to remove array elements
    By Mike in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 08:58

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.