Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. custom QSlider background using QSlider as overlay?

custom QSlider background using QSlider as overlay?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 1 Posters 877 Views
  • 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.
  • Kent-DorfmanK Offline
    Kent-DorfmanK Offline
    Kent-Dorfman
    wrote on last edited by
    #1

    The only other thing that comes up close is an "unsolved" post from a year ago where a guy wants to paint over a QSlider, without reimplementing the paint() method.

    My use case is to create a horizontal slider that I can mark in and out points with by the ends of the slider having different background colors than the normal slider background. I currently implement this as follows in python:

        QWidget.__init__(this, _parent)
            this.inBackground = QLabel(" ", this)
            this.inBackground.setAutoFillBackground(True)
            this.outBackground = QLabel(" ", this)
            this.outBackground.setAutoFillBackground(True)
            s = QSlider(0x01, this)
            s.setTickInterval(10)
            s.setTickPosition(QSlider.TicksBothSides)
            s.setStyleSheet("background-color: rgba(127,127,127,0)")
            s.setAutoFillBackground(True)
            this.slider = s
    

    and resize() as

        def resizeEvent(this, _e):
            s = this.size()
            l = this.slider.minimum()
            h = this.slider.maximum()
            r = float(h - l)
            w = int(s.width() * ( this.inValue - l) / r + 0.5)
            sz = QSize(w, s.height())
            this.inBackground.move(0, 0)
            this.inBackground.resize(sz)
            w = int(s.width() * ( this.outValue - l) / r + 0.5)
            sz = QSize(s.width() - w, s.height())
            this.outBackground.move(w, 0)
            this.outBackground.resize(sz)
            this.slider.resize(this.size())
    

    Works OK, EXCEPT that the tick marks disappear when I make the window active or resize it. Guessing it has to do with the update order of the subwidgets? Any elegant way to preserve the tick marks in a platform neutral manner that does not involve custom paint methods or a bunch of intermediate subclasses just to handle keeping the ticks visible?

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      Upon further examination, subclassing QSlider and reimplementing the painter is probably the most direct way of doing this...as long as I can chain the paint methods to call the parent method last and the parent method honors transparency/opacity. Will attempt and post results.

      1 Reply Last reply
      0
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        Yes, trivial paintEvent() override solves the problem of custom background. Aside from some geometry issues mapping the exact location where to change colors, the problem is solved...was just making it more complicated than it had to be.

        1 Reply Last reply
        0

        • Login

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