0

I want to set the groove color of a slider. The default is orange, below I try to set it to green but this does nothing .. what am I getting wrong? Maybe my naming is wrong. Can anyone correct the code below to set the lower portion of the slider to green?

import sys
from PyQt6 import QtWidgets, QtCore


class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        super().__init__()

        self.setFixedWidth(400)
        self.setFixedHeight(400)
        self.setStyleSheet("background: yellow")

        self.slider = QtWidgets.QSlider(QtCore.Qt.Orientation.Vertical)
        styles = "QSlider::sub-page:vertical { background: green; }"
        self.slider.setStyleSheet(styles)
        self.slider.setFixedHeight(300)
        self.slider.setMinimum(0)
        self.slider.setMaximum(100)
        self.slider.setValue(50)

        self.layout().addWidget(self.slider)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = MainWindow()
    ex.show()
    sys.exit(app.exec())
  • 1. Don't try to use the layout of a QMainWindow, use `setCentralWidget()` instead (possibly with a container QWidget with its own layout ); 2. **never** set generic properties for containers (including windows), as they propagate to *all* children; use [selector types](//doc.qt.io/qt-6/stylesheet-syntax.html#selector-types) instead; 3. as also written in [the docs](//doc.qt.io/qt-6/stylesheet-syntax.html#sub-controls): "With complex widgets such as QComboBox and QScrollBar, if one property or sub-control is customized, all the other properties or sub-controls must be customized as well." – musicamante Aug 23 '23 at 14:40

0 Answers0