2

I'm trying to increase the width of the Qslider handle to make it more user friendly on small screens, but I've only found ways of doing this in C++.

I have tried to implement this in python using the following code, only for it to change the colour of the slider:

    self.Zoom.setStyleSheet("""
             QSlider::handle:horizontal {
                                        height: 80px;
                                        }
            """)

Any advice on how to use QWidget.setStyleSheet() correctly.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Jkind9
  • 742
  • 7
  • 24
  • Check out this [Answer from @ekhumoro](https://stackoverflow.com/a/46796460/9705687). It seems you have to edit both the handle and the groove to get StyleSheet solutions to work. – bfris Jul 15 '20 at 17:23

2 Answers2

2

A possible solution is to use a QProxyStyle:

from PyQt5 import QtCore, QtWidgets


class SliderProxyStyle(QtWidgets.QProxyStyle):
    def pixelMetric(self, metric, option, widget):
        if metric == QtWidgets.QStyle.PM_SliderThickness:
            return 40
        elif metric == QtWidgets.QStyle.PM_SliderLength:
            return 40
        return super().pixelMetric(metric, option, widget)


class TestWindow(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)

        style = SliderProxyStyle(self.slider.style())
        self.slider.setStyle(style)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.slider)
        lay.addStretch()


if __name__ == "__main__":

    import sys

    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("fusion")
    w = TestWindow()
    w.show()
    app.exec_()

Before

enter image description here

After

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
0

A possible solution is to use a QSS:

from PyQt5 import QtCore, QtWidgets


class TestWindow(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
        self.slider.setMinimumHeight(70)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.slider)
        lay.addStretch()


QSS = """
/* QSlider --------------------------------------  */
QSlider::groove:horizontal {
    border-radius: 1px;
    height: 3px;
    margin: 0px;
    background-color: rgb(52, 59, 72);
}
QSlider::groove:horizontal:hover {
    background-color: rgb(55, 62, 76);
}
QSlider::handle:horizontal {
    background-color: rgb(85, 170, 255);
    border: none;
    height: 40px;
    width: 40px;
    margin: -20px 0;
    border-radius: 20px;
    padding: -20px 0px;
}
QSlider::handle:horizontal:hover {
    background-color: rgb(155, 180, 255);
}
QSlider::handle:horizontal:pressed {
    background-color: rgb(65, 255, 195);
}
"""


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("fusion")
    app.setStyleSheet(QSS)
    w = TestWindow()
    w.show()
    sys.exit(app.exec_())

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33