Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QStackedWidget biggest Widget displayed to small



  • I have the following Issue:

    A QStackedWidget in a class which manages changing between the pages of QStackedWidget:

    class MainWindow : public QWidget
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = nullptr);
    
    //...
    
    private:
        QStackedWidget *mStackedWidget;
    };
    

    The stacked Widget gets populated with widgets in the constructor:

    MainWindow::MainWindow(QWidget *parent)
        : QWidget{ parent },
          mStackedWidget{ new QStackedWidget }
    {
        auto startDialog = new StartDialog;
        auto game = new Game;
        auto gameOverDialog = new GameOverDialog;
    
    //...
    
        mStackedWidget->addWidget(startDialog);
        mStackedWidget->addWidget(game);
        mStackedWidget->addWidget(gameOverDialog);
    
        auto layout = new QHBoxLayout;
        layout->addWidget(mStackedWidget);
        setLayout(layout);
    }
    

    Now I want to have the MainWindow always display in the same Size. From what I understand normally QStackedWidget takes the size of the biggest Widget it manages and displays all the Widgets in that size.

    If I display in the constructor wuth qDebug the sizes of startDialog, game and gameOverDialog i get this output:

    startDialog->size() QSize(400, 300)
    game->size() QSize(640, 480)
    gameOverDialog->size() QSize(400, 300)
    

    However if I run the app and switch between the pages of QStackedWidget the page of game is always to small. To me it looks the size of startDialog or gameOverDialog is taken.

    What can I do?



  • I found out the Issue lies in my Game Widget.

    Game contains several Widgets. The Issue lies in the Dungeon widget it contains. With the Issue the dungeon code looked like this:

    class Game : public QWidget
    {
    //...
    private:
    // other widgets here
        Dungeon *mDungeon;
    
    class Dungeon : public QWidget
    {
        Q_OBJECT
    public:
        explicit Dungeon(QWidget *parent = nullptr);
    //..
    protected:
        void resizeEvent(QResizeEvent *event) override;
    //...
    private:
    //...
        void scaleViewToSize();
    
        QGraphicsScene *mGraphicsScene;
        DungeonView *mDungeonView;
    }
    
    Dungeon::Dungeon(QWidget *parent)
    //...
    {
    // Add all the objects into the scene and add the scene to the view
    
        scaleViewToSize();
    }
    
    void Dungeon::resizeEvent(QResizeEvent *event)
    {
        Q_UNUSED(event)
        scaleViewToSize();
    }
    
    void Dungeon::scaleViewToSize()
    {
        mDungeonView->fitInView(mDungeonView->scene()->sceneRect(),
                                 Qt::KeepAspectRatio);
    }
    

    I just realized when inspecting the code that i never whant to resize again because I add all the elements into the Dungeon in the constructor. Therefore I don't need to override resizeEvent.

    Instead I added to the constructor of Dungeon at the end:

    setFixedSize(size());
    

    Then it looks like QStackedWidget takes Game as the biggest Widget and resizes all the other Widgets after that.

    Still I don't understand completly why it was not working when I override resizeEvent.

    If Game is the first Widget in the QStackedWidget everything is fine. But If it comes second it gets resized wrong.



  • @sandro4912
    Here is my thought: when finding size of largest widget, widgets can look different when they are actually shown and sizes are calculated. When your Game is first widget is gets shown immediately, when it is second widget it does not get shown till you cause it to, sizing is initially taken from first widget shown.

    Presumably, if you leave it as second widget but set QStackedWidget initially so current page is the second one, then it works OK?



  • @JonB said in QStackedWidget biggest Widget displayed to small:

    Presumably, if you leave it as second widget but set QStackedWidget initially so current page is the second one, then it works OK?

    I also tryed that but still the display is wrong until i apply the fix mentioned above.


Log in to reply