Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QSlider groove color
Forum Updated to NodeBB v4.3 + New Features

QSlider groove color

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Captain Haddock
    wrote on last edited by Captain Haddock
    #1

    I add a QSlider and find when the window is inactive, the groove is blue and when active the groove is orange. How do I change those colors? I have seen various posts relating to this using styles sheets but have not managed to change the color. Any better pointers or more helpful, can anyone fix up the code below to use different colors for each state please?

    I may be getting confused by the terminology in other posts, e.g. how should I refer to the area that is colored orange?

    I tried modifying the styles of QSlider::add-page and QSlider::sub-page as shown here: https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qslider to no effect.

    import sys
    from PyQt6 import QtWidgets, QtCore
    
    
    class MainWindow(QtWidgets.QMainWindow):
    
        def __init__(self):
            super().__init__()
    
            self.setFixedWidth(400)
            self.setFixedHeight(400)
    
            self.slider = QtWidgets.QSlider(QtCore.Qt.Orientation.Vertical)
            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())
    
    
    SGaistS 1 Reply Last reply
    0
    • C Captain Haddock

      I add a QSlider and find when the window is inactive, the groove is blue and when active the groove is orange. How do I change those colors? I have seen various posts relating to this using styles sheets but have not managed to change the color. Any better pointers or more helpful, can anyone fix up the code below to use different colors for each state please?

      I may be getting confused by the terminology in other posts, e.g. how should I refer to the area that is colored orange?

      I tried modifying the styles of QSlider::add-page and QSlider::sub-page as shown here: https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qslider to no effect.

      import sys
      from PyQt6 import QtWidgets, QtCore
      
      
      class MainWindow(QtWidgets.QMainWindow):
      
          def __init__(self):
              super().__init__()
      
              self.setFixedWidth(400)
              self.setFixedHeight(400)
      
              self.slider = QtWidgets.QSlider(QtCore.Qt.Orientation.Vertical)
              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())
      
      
      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you show the stylesheet you use and how you applied it ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Can you show the stylesheet you use and how you applied it ?

        C Offline
        C Offline
        Captain Haddock
        wrote on last edited by
        #3

        @SGaist I found a solution for my case. The hint someone pointed to (on another board) was to set all the styles. The code below achieves what I was after with the original shown to the side (might not be the most elegant code) .
        Relevant links are:

        • https://stackoverflow.com/questions/76962373/pyqt6-qslider-how-to-set-groove-color (see comment "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." .. usability issue with that requirement I'd say!)

        • https://het.as.utexas.edu/HET/Software/html/stylesheet-examples.html#customizing-qslider

        import sys
        from PyQt6 import QtWidgets, QtCore
        
        
        class MainWindow(QtWidgets.QMainWindow):
        
            def __init__(self):
                super().__init__()
        
                self.setFixedWidth(400)
                self.setFixedHeight(400)
        
                frame = QtWidgets.QFrame()
                layout = QtWidgets.QHBoxLayout()
                frame.setLayout(layout)
        
                slider1 = QtWidgets.QSlider(QtCore.Qt.Orientation.Vertical)
                slider1.setFixedHeight(300)
                slider1.setFixedWidth(20)
                slider1.setMinimum(0)
                slider1.setMaximum(100)
                slider1.setValue(50)
        
                slider2 = QtWidgets.QSlider(QtCore.Qt.Orientation.Vertical)
                styles = "QSlider::groove:vertical { background: white; position: absolute; left: 8px; right: 7px; }"
                styles += "QSlider::handle:vertical { height: 11px; background: #979EA8; margin: 0 -4px; border-style:solid; border-color: grey;border-width:1px;border-radius:3px}"
                styles += "QSlider::sub-page:vertical { background: #979EA8; border-style:solid; border-color: grey;border-width:1px;border-radius:2px}"
                styles += "QSlider::add-page:vertical { background: #979EA8; border-style:solid; border-color: grey;border-width:1px;border-radius:2px}"
                slider2.setStyleSheet(styles)
                slider2.setFixedHeight(300)
                slider2.setFixedWidth(20)
                slider2.setMinimum(0)
                slider2.setMaximum(100)
                slider2.setValue(50)
        
                layout.addWidget(slider1)
                layout.addWidget(slider2)
                layout.addStretch(True)
        
                self.layout().addWidget(frame)
        
        if __name__ == '__main__':
            app = QtWidgets.QApplication(sys.argv)
            ex = MainWindow()
            ex.show()
            sys.exit(app.exec())
        
        
        1 Reply Last reply
        1
        • C Captain Haddock has marked this topic as solved on

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved