qt - 如何自定义 QSlider 中的手柄颜色?
问题描述
我无法弄清楚如何使用样式表在 QSlider 中自定义句柄。凹槽样式和手柄样式之间似乎存在一些干扰,我似乎无法弄清楚如何同时自定义两者。
这是我目前拥有的样式表:
stylesheet = """
QLabel {
font-family: Ubuntu-Regular;
font-size: 12px;
qproperty-alignment: AlignCenter;
color: %s;
background: %s;
border: 1px solid %s;
border-radius: 4px;
min-height: 40px;
max-height: 40px;
min-width: 48px;
max-width: 100px;
}
QSlider:horizontal {
min-width: 240px;
height: 13px;
}
QSlider:vertical {
min-height: 240px;
width: 13px;
}
QSlider::groove {
background: %s;
border-radius: 5px;
}
QSlider::handle {
background: %s;
border-radius: 5px;
}
QSlider::handle:horizontal {
min-width: 25px;
min-height: 13px;
}
QSlider::handle:vertical {
min-width: 13px;
min-height: 25px;
}
""" % (colors.gray_dark, colors.gray_light, colors.gray,
colors.gray_light, colors.primary1)
预期结果:
当前结果:
请注意句柄大小仅为 1 或 2 像素。
解决方案
试试看:
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QHBoxLayout, QLabel, QSlider
from PyQt5.QtCore import Qt
class MyStyle(QWidget):
def __init__(self):
super().__init__()
label = QLabel("123")
layout = QHBoxLayout(self)
layout.addWidget(QSlider(Qt.Horizontal))
layout.addWidget(label)
# style
CSS = """
QLabel {
font-family: Ubuntu-Regular;
font-size: 12px;
qproperty-alignment: AlignCenter;
color: yellow;
background: #565a5e;
border: 1px solid #565a5e;
border-radius: 4px;
min-height: 40px;
max-height: 40px;
min-width: 48px;
max-width: 100px;
}
QSlider::groove:horizontal {
border: 1px solid #565a5e;
height: 10px;
background: #eee;
margin: 0px;
border-radius: 4px;
}
QSlider::handle:horizontal {
background: red;
border: 1px solid #565a5e;
width: 24px;
height: 8px;
border-radius: 4px;
}
"""
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setStyleSheet(CSS)
w = MyStyle()
w.show()
sys.exit(app.exec_())
推荐阅读
- c# - Roslyn 代码修复测试调用 `VerifyDiagnostics` 也为固定代码样本,这使得测试永远不会成功
- reactjs - 如何修复“'nf' 未被识别为内部或外部命令”?
- php - 避免 WooCommerce Dokan 中父订单的订单完成通知
- javascript - 这个历史时区偏移 UTC+0057 是怎么回事?
- pandas - 替换熊猫数据框中 int64 末尾的数字
- node.js - 在该位置创建带有数字的目录
- java - Java Spring Boot:如何实现以 2 个字段作为参数的 Redis 查询?
- html - Catalina - 如何使用 file:// 打开 html 文件 - 沙盒 / 安全
- amazon-web-services - 在一个请求中获取文件夹 S3 Amazon 中的所有文件
- excel - 将不相邻的单元格转置到下一个开放行