如何在 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 像素。

S.尼克

尝试一下:

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_())

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章