Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QStackedWidget biggest Widget displayed to small

QStackedWidget biggest Widget displayed to small

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 795 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sandro4912
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sandro4912
      wrote on last edited by
      #2

      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.

      JonBJ 1 Reply Last reply
      0
      • S sandro4912

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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?

        S 1 Reply Last reply
        0
        • JonBJ JonB

          @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?

          S Offline
          S Offline
          sandro4912
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved