Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
custom QSlider background using QSlider as overlay?
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?
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.
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.