0

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

0

3 Answers 3

2
>>> 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.

1

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

2
  • 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. Sep 26, 2008 at 13:19
  • 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 Sep 26, 2008 at 13:23
0

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.