views:

654

answers:

3

Dear Stacktoverflow, can you show me an example of how to use a QScrollBar? Thanks.

+1  A: 

It will come down to you using the QScrollArea, it is a widget that implements showing something that is larger than the available space. You will not need to use QScrollBar directly. I don't have a PyQt example but there is a C++ example in the QT distribution it is called the "Image Viewer". The object hierarchy will still be the same

Harald Scheirich
Harald, thanks for the reply. I tried the QScrollArea, which seems simpler in my case. But I have a QFormLayout to put inside that QScrollArea, and this widget accepts only QWidgets. I created an empty QWidget around the QFormLayout, but then nothing is displayed.
Victor Noagbodji
Test the widget that you created with the layout first without the surrounding scroll area, if that displays correctly you will need to delve into the QScrollArea documentation, IMHO it is one of the more hairy parts of QT. In general a layout is not a widget, it is only formatting for the widget
Harald Scheirich
A: 

In the PyQT source code distribution, look at the file:

examples/widgets/sliders.pyw

Or there is a minimal example here (I guess I shouldn't copy paste because of potential copyright issues)

Jamie
+1  A: 
>>> import sys
>>> from PyQt4 import QtCore, QtGui
>>> app = QtGui.QApplication(sys.argv)
>>> sb = QtGui.QScrollBar()
>>> sb.setMinimum(0)
>>> sb.setMaximum(100)
>>> def on_slider_moved(value): print "new slider position: %i" % (value, )
>>> sb.connect(sb, QtCore.SIGNAL("sliderMoved(int)"), on_slider_moved)
>>> sb.show()
>>> app.exec_()

Now, when you move the slider (you might have to resize the window), you'll see the slider position printed to the terminal as you the handle.

Torsten Marek