[PyQt] Adding a layout to a QSlider widget

Mark Summerfield list at qtrac.plus.com
Wed Jun 23 14:51:18 BST 2010

On 2010-06-23, V. Armando Solé wrote:
> Hello,
> I am trying to create a widget consisting on a horizontal QSlider and an
> additional widget at its side. The widget should behave as a QSlider.
> I have tried to do it inheriting from a QSlider but the problem is I
> cannot get the additional widget at the side of the slider. It stays on
> top.
> It can be observed with the example below.
> Why the added layout overwrites the QSlider?
> Is there something I am missing or I have to implement everything
> starting from a QAbstractSlider?
> Thanks for your time.
> Best regards,
> Armando

I wouldn't inherit QSlider: instead I'd inherit QWidget and aggregate a
QSlider and a QLabel together:

from PyQt4 import QtCore, QtGui

class SliderWithBrowser(QtGui.QWidget):
    def __init__(self, *var, **kw):
        QtGui.QWidget.__init__(self, *var, **kw)
        self.slider = QtGui.QSlider()
        self.label = QtGui.QLabel(self)
        self.label.setText("This should not be on top of the Slider")
	# QVBoxLayout the label above; could use QHBoxLayout for
	# side-by-side
        layout = QtGui.QVBoxLayout()
def test(args):
if __name__=="__main__":
    import sys

The above works fine for me. And you should be able to use
SliderWithBrowser just like any other widget.

Mark Summerfield, Qtrac Ltd, www.qtrac.eu
    C++, Python, Qt, PyQt - training and consultancy
        "Programming in Python 3" - ISBN 0321680561

                            A true story...
 I ordered a Dell netbook with Ubuntu preinstalled. It arrived with no
 OS at all and Dell's "support" said the don't supply Ubuntu any more.
           Dell gave no apology, no solution, and no refund.
                     So I do *not* recommend Dell.

More information about the PyQt mailing list