Adding Scrollbars to a QFrame in Qt
September 10th, 2007 by exhuma.twn
Many things are very easy to accomplish in Qt. Qt’s designer is a great help with that. But the is no option to simply “enable” scrollbars for a frame. Also, the QScrollArea can nowhere be found in the designer. The solution is actually quite simple:
- Add the component you want to be scrollable (QFrame, QLabel, …) in the designer as usual
- In the application code:
- Create a new QScrollArea
- Set the parent of the widget you created in the designer to “None”
- Set the widget of the QScrollArea to the widget from step 1
- Insert the QScrollArea to you ui
And here’s some example code (boiled down to the essentials):
sa = QtGui.QScrollArea()
self.ui.myWidget.setParent(None)
sa.setWidget(self.ui.myWidget)
self.ui.vboxlayout1.insertWidget(0, sa)
self.ui.myWidget.setParent(None)
sa.setWidget(self.ui.myWidget)
self.ui.vboxlayout1.insertWidget(0, sa)
You would probably write this somewhere in you application-constructor right after you call setupUi.
This will insert the scroll area at the top of vboxlayout1 (see the doc for insertWidget). You can of course also use addWidget.
It’s a shame that there’s no direct support for QScrollArea in designer. Maybe we will see it in some future release.
An example for C++ can be found in the Qt Forums.
Posted in Python | 2 Comments »
