Search code examples
qtqt4pyqt4

Shared layout by widgets in QStackedWidget


I have a QStackedWidget with QWidgets. My QWidgets differ a bit, one has an additional button, other lacks a combo box and so on but all of them can be arranged in the same QGridLayout.

And that is exactly what I would like to achieve. I would like to have a QGridLayout in my QStackedWidget that is shared by all my QWidgets. Additionally my main window (QDockWidget) can be resized and I would like to set different column and row stretch for the grid layout.

Is there a clean solution for this?

I came up with two ideas.

One is to have a QGridLayout in each QWidget and connect them all together so that when one is resized, others do the same. However, the more QWidgets I had, the more complicated it would have been.

My second idea is to have one QGridLayout with QStackedWidget in each cell. The bigger the QGridLayout was, the harder it would be to maintain it.

None of my ideas seem to be good.

I am using PyQt4 but examples in C++ are welcome as well.


Solution

  • This doesn't seem to warrant anything more than maybe a shared addWidgetsToGridLayout function that can be used for each widget in the stack, which is a different object because it shows different things. Sharing widgets is bad in this setup, so make sure each widget in the stack has its own distinct widgets.

    I don't understand why you would want to resize invisible widgets on resize. When you switch to another widget on the stack, Qt will make sure the proper events are called to properly resize the layout.