滑动条点击定位
This commit is contained in:
parent
375efccc92
commit
23aae36c2a
5 changed files with 127 additions and 0 deletions
|
@ -63,6 +63,7 @@ encoding//\u591A\u9875\u9762/QScrollArea/\u4EFFQQ\u8BBE\u7F6E\u9762\u677F/\u4EFF
|
||||||
encoding//\u591A\u9875\u9762/QStackedWidget/\u5DE6\u4FA7\u9009\u9879\u5361/\u5DE6\u4FA7\u9009\u9879\u5361.py=utf-8
|
encoding//\u591A\u9875\u9762/QStackedWidget/\u5DE6\u4FA7\u9009\u9879\u5361/\u5DE6\u4FA7\u9009\u9879\u5361.py=utf-8
|
||||||
encoding//\u6811\u7ED3\u6784/QTreeWidget/Json\u751F\u6210QTreeWidget/Json\u751F\u6210\u6811\u5F62\u7ED3\u6784.py=utf-8
|
encoding//\u6811\u7ED3\u6784/QTreeWidget/Json\u751F\u6210QTreeWidget/Json\u751F\u6210\u6811\u5F62\u7ED3\u6784.py=utf-8
|
||||||
encoding//\u6D4F\u89C8\u5668/QWebView/\u68A6\u5E7B\u6811/\u68A6\u5E7B\u6811.py=utf-8
|
encoding//\u6D4F\u89C8\u5668/QWebView/\u68A6\u5E7B\u6811/\u68A6\u5E7B\u6811.py=utf-8
|
||||||
|
encoding//\u6ED1\u52A8\u6761/\u6ED1\u52A8\u6761\u70B9\u51FB\u5B9A\u4F4D.py=utf-8
|
||||||
encoding//\u7A97\u53E3/\u5206\u5272\u7A97\u53E3\u7684\u5206\u5272\u6761\u91CD\u5199.py=utf-8
|
encoding//\u7A97\u53E3/\u5206\u5272\u7A97\u53E3\u7684\u5206\u5272\u6761\u91CD\u5199.py=utf-8
|
||||||
encoding//\u7A97\u53E3/\u65E0\u8FB9\u6846\u81EA\u5B9A\u4E49\u6807\u9898\u680F\u7A97\u53E3/FramelessWindow.py=utf-8
|
encoding//\u7A97\u53E3/\u65E0\u8FB9\u6846\u81EA\u5B9A\u4E49\u6807\u9898\u680F\u7A97\u53E3/FramelessWindow.py=utf-8
|
||||||
encoding//\u7A97\u53E3/\u65E0\u8FB9\u6846\u81EA\u5B9A\u4E49\u6807\u9898\u680F\u7A97\u53E3/win\u65E0\u8FB9\u6846\u8C03\u6574\u5927\u5C0F.py=utf-8
|
encoding//\u7A97\u53E3/\u65E0\u8FB9\u6846\u81EA\u5B9A\u4E49\u6807\u9898\u680F\u7A97\u53E3/win\u65E0\u8FB9\u6846\u8C03\u6574\u5927\u5C0F.py=utf-8
|
||||||
|
|
|
@ -62,6 +62,10 @@
|
||||||
|
|
||||||
## [滚动条](滚动条)
|
## [滚动条](滚动条)
|
||||||
|
|
||||||
|
## [滑动条](滑动条)
|
||||||
|
|
||||||
|
1. [滑动条点击定位](滑动条/滑动条点击定位.py)
|
||||||
|
|
||||||
## [进度条](进度条)
|
## [进度条](进度条)
|
||||||
|
|
||||||
## [窗口](窗口)
|
## [窗口](窗口)
|
||||||
|
|
37
滑动条/README.md
Normal file
37
滑动条/README.md
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
# 滑动条
|
||||||
|
|
||||||
|
## [1、滑动条点击定位](滑动条点击定位.py)
|
||||||
|
|
||||||
|
1. `QSlider`对鼠标点击然后跳转到该位置的支持不是很好,通过重写鼠标点击事件`mousePressEvent`来达到效果
|
||||||
|
1. 通过`style`的`subControlRect`方法计算得到滑块的区域,当鼠标点击区域在此次时则交给系统自己处理(比如按住不放拖动)
|
||||||
|
1. 通过`orientation`判断滑动条的方向(横竖)
|
||||||
|
1. 通过`invertedAppearance`判断滑动条是否反向(左右、上下)
|
||||||
|
|
||||||
|
核心代码:
|
||||||
|
|
||||||
|
```python
|
||||||
|
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自行处理
|
||||||
|
super(JumpSlider, self).mousePressEvent(event)
|
||||||
|
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()))
|
||||||
|
```
|
||||||
|
|
||||||
|
![截图](ScreenShot/滑动条点击定位.gif)
|
BIN
滑动条/ScreenShot/滑动条点击定位.gif
Normal file
BIN
滑动条/ScreenShot/滑动条点击定位.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 41 KiB |
85
滑动条/滑动条点击定位.py
Normal file
85
滑动条/滑动条点击定位.py
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
"""
|
||||||
|
Created on 2018年11月5日
|
||||||
|
@author: Irony
|
||||||
|
@site: https://pyqt5.com https://github.com/892768447
|
||||||
|
@email: 892768447@qq.com
|
||||||
|
@file: Widgets.JumpSlider
|
||||||
|
@description:
|
||||||
|
"""
|
||||||
|
from PyQt5.QtCore import Qt
|
||||||
|
from PyQt5.QtWidgets import QSlider, QStyleOptionSlider, QStyle, QWidget,\
|
||||||
|
QFormLayout, QLabel
|
||||||
|
|
||||||
|
|
||||||
|
__Author__ = """By: Irony
|
||||||
|
QQ: 892768447
|
||||||
|
Email: 892768447@qq.com"""
|
||||||
|
__Copyright__ = "Copyright (c) 2018 Irony"
|
||||||
|
__Version__ = "Version 1.0"
|
||||||
|
|
||||||
|
|
||||||
|
class JumpSlider(QSlider):
|
||||||
|
|
||||||
|
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自行处理
|
||||||
|
super(JumpSlider, self).mousePressEvent(event)
|
||||||
|
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()))
|
||||||
|
|
||||||
|
|
||||||
|
class TestWindow(QWidget):
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(TestWindow, self).__init__(*args, **kwargs)
|
||||||
|
layout = QFormLayout(self)
|
||||||
|
|
||||||
|
self.label1 = QLabel('0', self)
|
||||||
|
layout.addRow(self.label1, JumpSlider(
|
||||||
|
Qt.Horizontal, valueChanged=lambda v: self.label1.setText(str(v))))
|
||||||
|
|
||||||
|
# 横向-反向显示
|
||||||
|
self.label2 = QLabel('0', self)
|
||||||
|
layout.addRow(self.label2, JumpSlider(
|
||||||
|
Qt.Horizontal, invertedAppearance=True,
|
||||||
|
valueChanged=lambda v: self.label2.setText(str(v))))
|
||||||
|
|
||||||
|
self.label3 = QLabel('0', self)
|
||||||
|
layout.addRow(self.label3, JumpSlider(
|
||||||
|
Qt.Vertical, minimumHeight=200, valueChanged=lambda v: self.label3.setText(str(v))))
|
||||||
|
|
||||||
|
# 纵向反向显示
|
||||||
|
self.label4 = QLabel('0', self)
|
||||||
|
layout.addRow(self.label4, JumpSlider(
|
||||||
|
Qt.Vertical, invertedAppearance=True,
|
||||||
|
minimumHeight=200, valueChanged=lambda v: self.label4.setText(str(v))))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import sys
|
||||||
|
import cgitb
|
||||||
|
sys.excepthook = cgitb.enable(1, None, 5, '')
|
||||||
|
from PyQt5.QtWidgets import QApplication
|
||||||
|
app = QApplication(sys.argv)
|
||||||
|
w = TestWindow()
|
||||||
|
w.show()
|
||||||
|
sys.exit(app.exec_())
|
Loading…
Reference in a new issue