QScrollArea and Custom Widget - No scrollbars
-
Hello everybody.
I know this question has been asked before but all those hints did not answer my question.
I have a QScrollArea as an Element on my Main Window. It gets resized with the windows itself.
Insided the Scrollarea I placed a custom widget that I created by inheriting from QWidget. This custom widget has a fixed size, but may change this size from time to time. I want the ScrollArea to show ScrollBars if the inner custom Widget is too big to fit.What commands do I have to add to a) my widget and b) the ScrollArea to achieve this?
Sadly, the hint in the docs about setting an appropriate Size Hint did not do anything for me.
My current Code looks like this:
Main Class:
@
ui.displayWidgetScrollArea->setWidgetResizable(true);
this->labyrinthWidget = new LabyrinthWidget(this->labyrinth, ui.displayWidgetScrollArea);
this->labyrinthWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
ui.displayWidgetScrollArea->setBackgroundRole(QPalette::Dark);
@Inside the LabyrinthWidget, called whenever the Widgets need to change its size:
@
int locWidth = someValueCalculatedHere;
int locHeight = someValueCalculatedHere;
this->resize(locWidth, locHeight);
this->setMinimumSize(locWidth, locHeight);
this->setMaximumSize(locWidth, locHeight);
this->update();
@Qt 5.0, MSVC2012 Update 1.
-
To add the widget to the scroll area, use QScrollArea::setWidget.
-
This simple and simplistic one sentence answer just nearly made me cry.
Yes, this is the solution, now it works just as expected.
Thanks a lot :) You made my day!@
ui.displayWidgetScrollArea->setWidgetResizable(true);
this->labyrinthWidget = new LabyrinthWidget(this->labyrinth, ui.displayWidgetScrollArea);
this->labyrinthWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
ui.displayWidgetScrollArea->setBackgroundRole(QPalette::Dark);
ui.displayWidgetScrollArea->setWidget(this->labyrinthWidget);
@A small question remains: Why was the widget displayed before? It was there and showed the correct contents...
-
Probably because of parenting. If you make a parent visible, the children will become visible.