Reputation: 1075
I cannot figure out how to customize a handle in a QSlider using stylesheets. There seems to be some interference between the groove style and the handle style, I cannot seem to figure out how to customize both at the same time.
This is the stylesheet I currently have:
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 result:
Current result:
Notice how the handle size is only 1 or 2px.
Upvotes: 2
Views: 2737
Reputation: 1075
Condensed answer: use width
and height
rather than min-width
and min-height
in QSlider::handle
.
QSlider::handle:horizontal {
width: 25px;
}
QSlider::handle:vertical {
height: 25px;
}
Upvotes: 1
Reputation: 13701
Try it:
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_())
Upvotes: 2