Results 1 to 3 of 3

Thread: Reorganize elements in a QGridLayout

  1. #1
    Join Date
    Feb 2013
    Posts
    38
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Reorganize elements in a QGridLayout

    Hi,

    I have a QGridLayout in a Widget. This layout is divided into four parts by placing four widgets in it.

    I want to be able to remove the four internal widgets and the just put one of them in the layout (which will occupy the whole space).

    I am currently using the following code to remove the elements in the layout (without destroying them because I want to reuse them later) :

    Qt Code:
    1. QLayoutItem *child;
    2. while((child = this->gridLayout->takeAt(0)) != 0);
    To copy to clipboard, switch view to plain text mode 

    And then I add the new widget in the layout.

    My problem is that the old items are still visible and the new one is just displayed on them...I don't understand why. The old widgets are displayed but they don't follow the layout rules (like they are not in it, they are just still "present" in the display).

    Any leads ?

    Thanks

    PS 1 : The widget are actually QwtPlot and here is what I see on the scale for example : scale.jpg
    PS 2 : Also, the same thing is observed when I delete the layout and create a new one. Some how, the old one is still visible.
    PS 3 : The problem can be seen with this code

    Qt Code:
    1. QWidget *w = new QWidget();
    2. w->setLayout(new QGridLayout());
    3. static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 0, 0);
    4. static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 0, 1);
    5. static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 1, 0);
    6. static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 1, 1);
    7. delete w->layout();
    8. w->setLayout(new QGridLayout());
    9. static_cast<QGridLayout*>(w->layout())->addWidget(new QLabel(QString("hey")), 0, 0);
    10. w->show();
    To copy to clipboard, switch view to plain text mode 

    Running that displays the label in the new label (in place) but there is still one old button in the background (it is unclickage, it is just drawn)...
    Last edited by moijhd; 26th March 2013 at 09:01.

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

    Default Re: Reorganize elements in a QGridLayout

    Looks you like you got most of the things correct, only thing missing is that you need to hide the widget.

    Removing the widget from the layout will only remove the widget from the layout (will not hide it), the removed widget will still be a child of the layout's parent widget, which means that the removed widget is not in layout but is still in it's parent widget, hence will not follow the layout. (which is what you are seeing)
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Feb 2013
    Posts
    38
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Reorganize elements in a QGridLayout

    Thanks, so the following code works :

    Qt Code:
    1. QWidget *w = new QWidget();
    2. w->setLayout(new QGridLayout());
    3. static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 0, 0);
    4. static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 0, 1);
    5. static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 1, 0);
    6. static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 1, 1);
    7. for(int i = 0; i < w->layout()->count(); i++)
    8. {
    9. w->layout()->itemAt(i)->widget()->hide();
    10. }
    11. delete w->layout();
    12. w->setLayout(new QGridLayout());
    13. static_cast<QGridLayout*>(w->layout())->addWidget(new QLabel(QString("hey")), 0, 0);
    14. w->show();
    To copy to clipboard, switch view to plain text mode 

    I'll try in my case

    PS : this also works if I don't delete and create a layout.


    Added after 9 minutes:


    Okay it seems to work so far.

    One more thing. Of course, I don't have only widgets in my layout but I also have sub layouts. So I have to hide the widgets they contain so that they can them self get reduced to 0 ? Or is there an alternative method (except putting the sub layout in a widget and add the widget instead of the layout...) ?
    Last edited by moijhd; 26th March 2013 at 09:48.

Similar Threads

  1. Print QML elements
    By merajc in forum Qt Quick
    Replies: 0
    Last Post: 15th August 2012, 23:04
  2. Ready to use QML elements
    By .:saeed:. in forum Qt Quick
    Replies: 0
    Last Post: 22nd February 2011, 16:25
  3. Ui Elements (QLineEdit) + QML
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2010, 16:04
  4. Delete a QGridLayout and New QGridLayout at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2007, 13:01
  5. Elements out of Range!!
    By Kapil in forum Newbie
    Replies: 9
    Last Post: 3rd April 2006, 10:28

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
  •  
Qt is a trademark of The Qt Company.