2018-11-05 22:55:57 +08:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Created on 2018年11月5日
|
|
|
|
|
@author: Irony
|
2021-07-13 14:52:26 +08:00
|
|
|
|
@site: https://pyqt.site , https://github.com/PyQt5
|
2018-11-05 22:55:57 +08:00
|
|
|
|
@email: 892768447@qq.com
|
2018-12-26 23:04:56 +08:00
|
|
|
|
@file: ClickJumpSlider
|
2018-11-05 22:55:57 +08:00
|
|
|
|
@description:
|
|
|
|
|
"""
|
|
|
|
|
|
2021-07-13 14:52:26 +08:00
|
|
|
|
try:
|
|
|
|
|
from PyQt5.QtCore import Qt
|
|
|
|
|
from PyQt5.QtWidgets import QApplication, QSlider, QStyleOptionSlider, QStyle, QWidget, QFormLayout, \
|
|
|
|
|
QLabel
|
|
|
|
|
except ImportError:
|
|
|
|
|
from PySide2.QtCore import Qt
|
|
|
|
|
from PySide2.QtWidgets import QApplication, QSlider, QStyleOptionSlider, QStyle, QWidget, QFormLayout, \
|
|
|
|
|
QLabel
|
2018-11-05 22:55:57 +08:00
|
|
|
|
|
|
|
|
|
|
2018-12-26 23:04:56 +08:00
|
|
|
|
class ClickJumpSlider(QSlider):
|
2018-11-05 22:55:57 +08:00
|
|
|
|
|
|
|
|
|
def mousePressEvent(self, event):
|
|
|
|
|
# 获取上面的拉动块位置
|
|
|
|
|
option = QStyleOptionSlider()
|
|
|
|
|
self.initStyleOption(option)
|
|
|
|
|
rect = self.style().subControlRect(
|
|
|
|
|
QStyle.CC_Slider, option, QStyle.SC_SliderHandle, self)
|
|
|
|
|
if rect.contains(event.pos()):
|
|
|
|
|
# 如果鼠标点击的位置在滑块上则交给Qt自行处理
|
2018-12-26 23:04:56 +08:00
|
|
|
|
super(ClickJumpSlider, self).mousePressEvent(event)
|
2018-11-05 22:55:57 +08:00
|
|
|
|
return
|
|
|
|
|
if self.orientation() == Qt.Horizontal:
|
|
|
|
|
# 横向,要考虑invertedAppearance是否反向显示的问题
|
|
|
|
|
self.setValue(self.style().sliderValueFromPosition(
|
|
|
|
|
self.minimum(), self.maximum(),
|
|
|
|
|
event.x() if not self.invertedAppearance() else (self.width(
|
|
|
|
|
) - event.x()), self.width()))
|
|
|
|
|
else:
|
|
|
|
|
# 纵向
|
|
|
|
|
self.setValue(self.style().sliderValueFromPosition(
|
|
|
|
|
self.minimum(), self.maximum(),
|
|
|
|
|
(self.height() - event.y()) if not self.invertedAppearance(
|
|
|
|
|
) else event.y(), self.height()))
|
|
|
|
|
|
|
|
|
|
|
2018-12-26 23:04:56 +08:00
|
|
|
|
class DemoWindow(QWidget):
|
2018-11-05 22:55:57 +08:00
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2018-12-26 23:04:56 +08:00
|
|
|
|
super(DemoWindow, self).__init__(*args, **kwargs)
|
|
|
|
|
self.resize(600, 600)
|
2018-11-05 22:55:57 +08:00
|
|
|
|
layout = QFormLayout(self)
|
|
|
|
|
|
|
|
|
|
self.label1 = QLabel('0', self)
|
2021-07-13 14:52:26 +08:00
|
|
|
|
self.slider1 = ClickJumpSlider(Qt.Horizontal)
|
|
|
|
|
self.slider1.valueChanged.connect(lambda v: self.label1.setText(str(v)))
|
|
|
|
|
layout.addRow(self.label1, self.slider1)
|
2018-11-05 22:55:57 +08:00
|
|
|
|
|
|
|
|
|
# 横向-反向显示
|
|
|
|
|
self.label2 = QLabel('0', self)
|
2021-07-13 14:52:26 +08:00
|
|
|
|
self.slider2 = ClickJumpSlider(Qt.Horizontal, invertedAppearance=True)
|
|
|
|
|
self.slider2.valueChanged.connect(lambda v: self.label2.setText(str(v)))
|
|
|
|
|
layout.addRow(self.label2, self.slider2)
|
2018-11-05 22:55:57 +08:00
|
|
|
|
|
|
|
|
|
self.label3 = QLabel('0', self)
|
2021-07-13 14:52:26 +08:00
|
|
|
|
self.slider3 = ClickJumpSlider(Qt.Vertical, minimumHeight=200)
|
|
|
|
|
self.slider3.valueChanged.connect(lambda v: self.label3.setText(str(v)))
|
|
|
|
|
layout.addRow(self.label3, self.slider3)
|
2018-11-05 22:55:57 +08:00
|
|
|
|
|
|
|
|
|
# 纵向反向显示
|
|
|
|
|
self.label4 = QLabel('0', self)
|
2021-07-13 14:52:26 +08:00
|
|
|
|
self.slider4 = ClickJumpSlider(Qt.Vertical, invertedAppearance=True, minimumHeight=200)
|
|
|
|
|
self.slider4.valueChanged.connect(lambda v: self.label4.setText(str(v)))
|
|
|
|
|
layout.addRow(self.label4, self.slider4)
|
2018-11-05 22:55:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
import sys
|
|
|
|
|
import cgitb
|
2021-07-13 14:52:26 +08:00
|
|
|
|
|
|
|
|
|
cgitb.enable(format='text')
|
|
|
|
|
|
2018-11-05 22:55:57 +08:00
|
|
|
|
app = QApplication(sys.argv)
|
2018-12-26 23:04:56 +08:00
|
|
|
|
w = DemoWindow()
|
2018-11-05 22:55:57 +08:00
|
|
|
|
w.show()
|
|
|
|
|
sys.exit(app.exec_())
|