Important: Please read the Qt Code of Conduct -

How do we rearrange widgets added to a Grid Layout at runtime?

  • How do we rearrange or remove a widget added to QGridLayout in runtime. Say in portrait mode I wish to show two labels each in two rows and in landscape mode it should change to all four labels in single row.

  • AFAIK using Grid Layout it can be done only manually (so you need to manually catch mode change and rearrange elements). Maybe QML is an option? It can be done slighlty easy there.

  • I can detect mode change. But how to rearrange the labels inside gridlayout. I couldn't locate any api for removing or rearranging the widgets added in the GridLayout.

    QML is still in its early stages so don't want to try that now.

  • You can use two different layouts for landscape and portrait modes I think. No idea about other ways, I'm using qml for mobile UIs and it fits all my needs :).

  • If using two different layouts, how should we switch it. Can we hide a layout and all its controls.

  • [quote author="QtK" date="1287735292"]If using two different layouts, how should we switch it. Can we hide a layout and all its controls.[/quote]

    Do you want to keep 2 different layouts and then switch them by just hiding one and showing the other?

  • I don't prefer that but still would like to know how to do that if that is possible.

  • When you detect the mode change, call a slot that uses QLayout::removeWidget() to remove your widgets. Then add them to your new layout and finally set the new layout on the parent widget.

  • Thank you, I was looking for something like QLayout::removeWidget(). I will try something like what you have suggested above.

    Also is there any other better solution available for handling orientation change.

  • Have you seen this "blog": entry from last year? I really would like ot see this functionality included into Qt but you might be able to get the code for this research project somewhere.

    I am also interested in tracking this down. If I find it I'll post a comment on here too.

  • [quote author="QtK" date="1287741731"]Thank you, I was looking for something like QLayout::removeWidget()[/quote]

    you could alternatively use QLayout::removeItem(), and then add them back with QLayout::addItem().. you probably already also know this by now :)

Log in to reply