| 21 | |
| 22 | |
| 23 | class ClickJumpSlider(QSlider): |
| 24 | |
| 25 | def mousePressEvent(self, event): |
| 26 | # 获取上面的拉动块位置 |
| 27 | option = QStyleOptionSlider() |
| 28 | self.initStyleOption(option) |
| 29 | rect = self.style().subControlRect( |
| 30 | QStyle.CC_Slider, option, QStyle.SC_SliderHandle, self) |
| 31 | if rect.contains(event.pos()): |
| 32 | # 如果鼠标点击的位置在滑块上则交给Qt自行处理 |
| 33 | super(ClickJumpSlider, self).mousePressEvent(event) |
| 34 | return |
| 35 | if self.orientation() == Qt.Horizontal: |
| 36 | # 横向,要考虑invertedAppearance是否反向显示的问题 |
| 37 | self.setValue(self.style().sliderValueFromPosition( |
| 38 | self.minimum(), self.maximum(), |
| 39 | event.x() if not self.invertedAppearance() else (self.width( |
| 40 | ) - event.x()), self.width())) |
| 41 | else: |
| 42 | # 纵向 |
| 43 | self.setValue(self.style().sliderValueFromPosition( |
| 44 | self.minimum(), self.maximum(), |
| 45 | (self.height() - event.y()) if not self.invertedAppearance( |
| 46 | ) else event.y(), self.height())) |
| 47 | |
| 48 | |
| 49 | class DemoWindow(QWidget): |