1 Star 2 Fork 0

透明2002/PyQt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

QSlider

1、滑动条点击定位

运行 ClickJumpSlider.py

  1. QSlider对鼠标点击然后跳转到该位置的支持不是很好,通过重写鼠标点击事件mousePressEvent来达到效果
  2. 通过stylesubControlRect方法计算得到滑块的区域,当鼠标点击区域在此次时则交给系统自己处理(比如按住不放拖动)
  3. 通过orientation判断滑动条的方向(横竖)
  4. 通过invertedAppearance判断滑动条是否反向(左右、上下)
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()))

ClickJumpSlider

2、双层圆环样式

运行 QssQSlider.py | 运行 PaintQSlider.py

QssQSlider PaintQSlider

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/won2930015/PyQt.git
git@gitee.com:won2930015/PyQt.git
won2930015
PyQt
PyQt
master

搜索帮助