1

I'm writing an app for a touchscreen, and the default handle is too small to grab reliably, so I want to make it bigger. According to the official documentation, several answers on SE, and a couple of other forums, this ought to work in a QWidget's constructor:

sliderGrandMaster = new QSlider(Qt::Vertical, panelRight);
sliderGrandMaster->setGeometry(          appdata->buttonBorder ,    //Left
                   buttonTopRight +      appdata->buttonBorder ,    //Top
                        halfwidth - (2 * appdata->buttonBorder),    //Width
            buttonRemainingHeight - (2 * appdata->buttonBorder));   //Height
sliderGrandMaster->setRange(0, RANGE_MAX);
sliderGrandMaster->setTickInterval(RANGE_MAX / 10);
sliderGrandMaster->setTickPosition(QSlider::TicksBothSides);
QString temp =  QString("handle:vertical { background: green; height: %1px; margin: 0 -%2px; }")
                .arg(buttonRemainingHeight / 5)
                .arg(halfwidth / 3);
sliderGrandMaster->setStyleSheet(temp);

But it seems to have no effect. The handle is the same small size regardless of what values I put in the stylesheet, and it's not even green.

With my values at runtime, temp ends up being handle:vertical { background: green; height: 66px; margin: 0 -32px; }. The size of the slider is 94px wide by 331px tall.

Am I missing something?


Edit:

This:

QString temp =  QString("QSlider::handle { background: green; height: %1px; width: %1px; margin: 0 -%2px; }")
                .arg(buttonRemainingHeight / 5)
                .arg(halfwidth / 3);
sliderGrandMaster->setStyleSheet(temp);

at least got it green. But the size is still wrong. Qt version 5.4.2

4

2 回答 2

3

您只需使用简单的样式表即可更改句柄的大小。例子 :

QS滑块

使用以下样式表完成:

QSlider::groove:horizontal
{
    border:none;
    margin-top: 10px;
    margin-bottom: 10px;
    height: 10px;
}  

QSlider::sub-page
{
    background: rgb(164, 192, 2);
}

QSlider::add-page 
{
    background: rgb(70, 70, 70);
}

QSlider::handle
{
    background: white;
    border: 3px solid black;
    width: 60px; // **Change the width here**
    margin: -30px 0;
}
于 2016-06-28T09:06:14.277 回答
2

好的,使用 Qt 5.6.1 我在这方面取得了一些进展。以下代码

QSlider* slider = new QSlider(this);
slider->setOrientation(Qt::Horizontal);
slider->setStyleSheet("QSlider::groove:horizontal { "
                      "border: 1px solid #999999; "
                      "height: 20px; "
                      "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4); "
                      "margin: 2px 0; "
                      "} "
                      "QSlider::handle:horizontal { "
                      "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f); "
                      "border: 1px solid #5c5c5c; "
                      "width: 30px; "
                      "margin: -2px 0px; "
                      "} ");

QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(slider);
layout->addWidget(new QSlider(Qt::Horizontal, this));
setLayout(layout);

可能会被放置到一个 emtyQWidget中工作。它只是添加了两个QSliders,其中一个是使用样式表修改的。

如您所见,我曾经也更改过凹槽,它按预期工作。但是有一些奇怪的问题可能是一个错误。尝试注释掉凹槽样式表的一些属性,如边框背景边距。如果没有有效的属性,则手柄的宽度属性被丢弃。由于我在文档中找不到任何约束,我想知道为什么会发生这种情况。

还要注意在手柄周围绘制边框的问题。似乎需要更多的微调。;)

滑块的屏幕截图


为您总结一下快速解决方案应该为凹槽添加一些默认属性。

于 2016-06-28T09:52:39.753 回答