0

I try to implement an auto-resizing QStackedLayout: what I would like is that the QStackedLayout change to the preferred size of the current shown widget. In addition, this QStackedLayout contains, among other widgets, ones whose the size can change during runtime (labels displaying a pixmap that can change but when the label in question is not displayed).

Therefore, I have overridden the method QStackedLayout::sizeHint() with the following implementation:

class StackedLayout : public QStackedLayout
{
    Q_OBJECT

public:
    using QStackedLayout::QStackedLayout;
public:
    QSize sizeHint() const override { return currentWidget()->sizeHint(); }
};

Then, I call QWidget::adjustSize() in the parent widget in order to follow the new "sizeHint" when I change current widget.

However, in reality, the StackedLayout instance adjust its size to the one computed with the first size of the pixmap: the currentWidget()->layout()->sizeHint() does not seem to be updated when the pixmap (and its size) change. But, what is a bit strange is that when I return a second time to the page, the correct behavior occurs (the currentWidget()->layout()->sizeHint() is updated and the widget is correctly resized).

I have firstly thought that I have to force the update by calling currentWidget()->layout()->update() just before QWidget::adjustSize() but it does not change anything.

Maybe, this issue is due to a misunderstanding of the behavior of QLayout::sizeHint()?

Beatussum
  • 1
  • 1
  • Could you please provide a small program as an example of your problem? I would prefer being able to test it directly, as it's easier to address the problem and fix it. It's hard trying to guess what's wrong from a description. – Abderrahmene Rayene Mihoub Jul 09 '23 at 21:44

0 Answers0