Как я могу настроить цвет ручки в QSlider? - PullRequest
0 голосов
/ 17 января 2019

Я не могу понять, как настроить дескриптор в 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)

Ожидаемый результат:

expected

Текущий результат:

expected

Обратите внимание, что размер ручки составляет всего 1 или 2 пикселя.

Ответы [ 2 ]

0 голосов
/ 17 января 2019

Сокращенный ответ: используйте width и height вместо min-width и min-height в QSlider::handle.

    QSlider::handle:horizontal {
        width: 25px;
    }

    QSlider::handle:vertical {
        height: 25px;
    }
0 голосов
/ 17 января 2019

Попробуйте:

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

enter image description here

...