QStackedWidget biggest Widget displayed to small
-
I have the following Issue:
A
QStackedWidgetin a class which manages changing between the pages ofQStackedWidget: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
MainWindowalways display in the same Size. From what I understand normallyQStackedWidgettakes the size of the biggestWidgetit manages and displays all theWidgetsin that size.If I display in the constructor wuth
qDebugthe sizes ofstartDialog,gameandgameOverDialogi 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
QStackedWidgetthe page ofgameis always to small. To me it looks the size ofstartDialogorgameOverDialogis taken.What can I do?
-
I found out the Issue lies in my
GameWidget.Game contains several Widgets. The Issue lies in the
Dungeonwidget 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
Dungeonin the constructor. Therefore I don't need to overrideresizeEvent.Instead I added to the constructor of
Dungeonat the end:setFixedSize(size());Then it looks like QStackedWidget takes
Gameas the biggestWidgetand resizes all the otherWidgetsafter that.Still I don't understand completly why it was not working when I override
resizeEvent.If
Gameis the firstWidgetin theQStackedWidgeteverything is fine. But If it comes second it gets resized wrong. -
I found out the Issue lies in my
GameWidget.Game contains several Widgets. The Issue lies in the
Dungeonwidget 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
Dungeonin the constructor. Therefore I don't need to overrideresizeEvent.Instead I added to the constructor of
Dungeonat the end:setFixedSize(size());Then it looks like QStackedWidget takes
Gameas the biggestWidgetand resizes all the otherWidgetsafter that.Still I don't understand completly why it was not working when I override
resizeEvent.If
Gameis the firstWidgetin theQStackedWidgeteverything 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 yourGameis 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
QStackedWidgetinitially so current page is the second one, then it works OK? -
@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 yourGameis 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
QStackedWidgetinitially 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.