QStackedWidget biggest Widget displayed to small
-
I have the following Issue:
A
QStackedWidget
in 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
MainWindow
always display in the same Size. From what I understand normallyQStackedWidget
takes the size of the biggestWidget
it manages and displays all theWidgets
in that size.If I display in the constructor wuth
qDebug
the sizes ofstartDialog
,game
andgameOverDialog
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 ofgame
is always to small. To me it looks the size ofstartDialog
orgameOverDialog
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 overrideresizeEvent
.Instead I added to the constructor of
Dungeon
at the end:setFixedSize(size());
Then it looks like QStackedWidget takes
Game
as the biggestWidget
and resizes all the otherWidgets
after that.Still I don't understand completly why it was not working when I override
resizeEvent
.If
Game
is the firstWidget
in theQStackedWidget
everything is fine. But If it comes second it gets resized wrong. -
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 overrideresizeEvent
.Instead I added to the constructor of
Dungeon
at the end:setFixedSize(size());
Then it looks like QStackedWidget takes
Game
as the biggestWidget
and resizes all the otherWidgets
after that.Still I don't understand completly why it was not working when I override
resizeEvent
.If
Game
is the firstWidget
in theQStackedWidget
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 yourGame
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? -
@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 yourGame
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.