且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用样式表更改 QSlider 句柄宽度

更新时间:2023-02-12 13:31:33

为了让它起作用,我发现必须在凹槽组件上设置边框.此外,为了使手柄大于凹槽,必须在手柄上设置负边距(参见 Qt 文档中的自定义 QSlider):

To get this to work, I found that a border must be set on the groove component. Also, to make the handle larger than the groove, a negative margin has to be set on the handle (see Customizing QSlider in the Qt Docs):

QSlider::groove:horizontal {
    border: 1px solid;
    height: 10px;
    margin: 0px;
    }
QSlider::handle:horizontal {
    background-color: black;
    border: 1px solid;
    height: 40px;
    width: 40px;
    margin: -15px 0px;
    }

PS:如果将滑块添加到布局中,则可能需要设置其最小高度以防止其被压缩回原始尺寸.

PS: If the slider is added to a layout, it might be necessary to set its minimum height in order to stop it being squashed back to its original dimensions.