3

How can I add a scroll bar to a QMainWindow, when this QMainWindow contains just one central widget, which is bigger than the QMainWindow size?

So that the scroll bar can be used to see different parts of this central widget.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Ahmad
  • 12,886
  • 30
  • 93
  • 146

2 Answers2

4

Set the central widget of your QMainWindow to a QScrollArea and then set the widget of that new QScrollArea to the widget that was previously your central widget.

Remember to set the "widget resizable" property of QScrollArea to true.

ianmac45
  • 2,258
  • 2
  • 19
  • 15
  • Alright I just tried this .. works sort of .. the original widget is displaying, but the scrollbars don't appear even when the size of the originally central widget exceeds the size of the QMainWindow .. – Ahmad Jun 25 '10 at 12:38
  • @Ahmad try using setHorizontalScrollBarPolicy() and setVerticalScrollBarPolicy() on your QScrollArea. – Casey Jun 25 '10 at 17:33
  • @Casey I'm still no luck with it. I think the original central widget is not being set properly to trigger the scroll bar. Any idea? – shiami Nov 13 '12 at 03:50
  • Hello, ianmac45, thank you for your help. Could you please provide an example of what you are saying? I would appreciate that. Thank you! – Angie Quijano Sep 08 '15 at 15:42
0

For some reason the child widget would not render at all if it was a QWidget with a layout. It only worked after calling setWidgetResizable(true).

auto mainWidget = new QWidget();
auto scrollArea = new QScrollArea();
scrollArea->setWidget(mainWidget);
scrollArea->setWidgetResizable(true);
setCentralWidget(scrollArea);
resize(1470, 900);

QHBoxLayout* mainLayout = new QHBoxLayout(mainWidget);
...
Andreas Haferburg
  • 5,189
  • 3
  • 37
  • 63