def paintEvent(self, event): # based on # https://github.com/qt/qtbase/blob/f40dbe0d0b54ce83d2168e82905cf4f75059a841/src/widgets/widgets/qslider.cpp#L315 # https://github.com/enthought/traitsui/blob/master/traitsui/qt4/extra/range_slider.py painter = QStylePainter(self) minpos = self.__min_position maxpos = self.__max_position # Draw the groove opt = QStyleOptionSlider() self.initStyleOption(opt) # Draw empty grove opt.sliderPosition = opt.minimum opt.subControls = QStyle.SC_SliderGroove if self.tickPosition() != self.NoTicks: opt.subControls |= QStyle.SC_SliderTickmarks painter.drawComplexControl(QStyle.CC_Slider, opt) # Draw the highlighted part on top # Qt4.8 and Qt5.3 draw the highlighted groove in a weird way because they # transpose opt.rect. Qt5.7 works fine. if QT_VERSION_STR >= '5.7.0': opt.subControls = QStyle.SC_SliderGroove opt.sliderPosition = opt.maximum if self.orientation() == Qt.Horizontal: _w = opt.rect.width() / opt.maximum x = round(_w * minpos) w = round(_w * (maxpos - minpos)) opt.rect = QRect(x, 0, w, opt.rect.height()) else: _h = opt.rect.height() / opt.maximum y = round(_h * minpos) h = round(_h * (maxpos - minpos)) opt.rect = QRect(0, y, opt.rect.width(), h) painter.drawComplexControl(QStyle.CC_Slider, opt) # Draw the handles for i, position in enumerate((minpos, maxpos)): opt = QStyleOptionSlider() self.initStyleOption(opt) opt.subControls = QStyle.SC_SliderHandle if self.__pressed_control and (self.__active_slider == i or self.__active_slider < 0): opt.activeSubControls = self.__pressed_control opt.state |= QStyle.State_Sunken else: opt.activeSubControls = self.__hovered_control opt.sliderPosition = position opt.sliderValue = position painter.drawComplexControl(QStyle.CC_Slider, opt)
def paintEvent(self, event): # based on # https://github.com/qt/qtbase/blob/f40dbe0d0b54ce83d2168e82905cf4f75059a841/src/widgets/widgets/qslider.cpp#L315 # https://github.com/enthought/traitsui/blob/master/traitsui/qt4/extra/range_slider.py painter = QStylePainter(self) minpos = self._min_position maxpos = self._max_position # Draw the groove opt = QStyleOptionSlider() self.initStyleOption(opt) # Draw empty grove opt.sliderPosition = opt.minimum opt.subControls = QStyle.SC_SliderGroove if self.tickPosition() != self.NoTicks: opt.subControls |= QStyle.SC_SliderTickmarks painter.drawComplexControl(QStyle.CC_Slider, opt) # Draw the highlighted part on top # Qt4.8 and Qt5.3 draw the highlighted groove in a weird way because they # transpose opt.rect. Qt5.7 works fine. if QT_VERSION_STR >= '5.7.0': opt.subControls = QStyle.SC_SliderGroove opt.sliderPosition = opt.maximum if self.orientation() == Qt.Horizontal: _w = opt.rect.width() / opt.maximum x = round(_w * minpos) w = round(_w * (maxpos - minpos)) opt.rect = QRect(x, 0, w, opt.rect.height()) else: _h = opt.rect.height() / opt.maximum y = round(_h * minpos) h = round(_h * (maxpos - minpos)) opt.rect = QRect(0, y, opt.rect.width(), h) painter.drawComplexControl(QStyle.CC_Slider, opt) # Draw the handles for i, position in enumerate((minpos, maxpos)): opt = QStyleOptionSlider() self.initStyleOption(opt) opt.subControls = QStyle.SC_SliderHandle if self.__pressed_control and (self.__active_slider == i or self.__active_slider < 0): opt.activeSubControls = self.__pressed_control opt.state |= QStyle.State_Sunken else: opt.activeSubControls = self.__hovered_control opt.sliderPosition = position opt.sliderValue = position painter.drawComplexControl(QStyle.CC_Slider, opt)
def paintEvent(self, e): #print("hover: ",self._hover,",press: ",self._press) painter = QStylePainter(self) # prepare the drawing options # for drawing slider groove opt = QStyleOptionSlider() opt.initFrom(self) opt.subControls = QStyle.SC_SliderGroove opt.maximum = self.__steps opt.minimum = 0 opt.orientation = self.orientation() opt.sliderPosition = self.__position0 opt.sliderValue = self.__value0 if self._tickList is not None: opt.tickPosition = QSlider.TicksBelow else: opt.tickPosition = QSlider.NoTicks handleWidth = self._handleWidth measureWidth = self.width() - handleWidth - handleWidth painter.drawComplexControl(QStyle.CC_Slider, opt) # draw tickmarks if self._tickList is not None: painter.drawPixmap(self._handleWidth, self.height() - self._tickHeight, self._tickImage) # handle colors colorPRESS = QColor(204, 204, 204) colorHOVER = QColor(23, 23, 23) colorNORMAL = QColor(0, 122, 217) if self.isEnabled() else colorPRESS handleHeight = self.height( ) if self._tickList is None else self.height() - self._tickHeight # draw handle 0 if self._press in {0, 2}: color = colorPRESS elif self._hover == 0: color = colorHOVER else: color = colorNORMAL self._rects[0] = r0 = QRect( self.__position0 / self.__steps * measureWidth, 0, handleWidth, handleHeight) painter.fillRect(self._rects[0], color) # draw handle 1 if self._press in {1, 2}: color = colorPRESS elif self._hover == 1: color = colorHOVER else: color = colorNORMAL self._rects[1] = r1 = QRect( self.__position1 / self.__steps * measureWidth + handleWidth, 0, handleWidth, handleHeight) painter.fillRect(self._rects[1], color) # draw inner handle if self._press == 2 or not self.isEnabled(): color = QColor(0, 0, 0, 15) elif self._hover == 2: color = QColor(0, 61, 108, 63) else: color = QColor(0, 122, 217, 63) self._rects[2] = QRect(r0.left(), r0.top(), r1.right() - r0.left() + 1, handleHeight) painter.fillRect(self._rects[2], color)