Qt Widgets setStyleSheet

4

由于我正在为平板电脑环境编程,一些小部件对于可触摸交互来说太小了。我想要特别放大QSpinBox按钮。

经过一些搜索,我发现唯一的方法似乎是使用setStyleSheet函数,但是它没有很好地解释如何使用。在这个链接到Qt参考文档中,描述了我应该传递给setStyleSheet函数的内容,但是不清楚是否可以只使用其中的一部分,还是即使我只想改变一部分,也必须使用全部内容。

无论如何,无论是使用部分还是整个文本,我的QDoubleSpinBox在应用这些设置后仍然保持默认状态。它甚至比我在ui文件中绘制的还要小。当然它没有将背景画成黄色(只是为了检查是否有效)或使按钮更大。

我正在使用这个函数:

customizeSpinBox(ui.doubleSpinBoxDistanciaLocalizador);
                ui.doubleSpinBoxDistanciaLocalizador->setVisible(true);

void MyClass::customizeSpinBox(QDoubleSpinBox *spinBox){

qApp->setStyleSheet("QSpinBox { border: 3px solid red; border-radius: 5px; background-color: yellow; }"
                       "QSpinBox::up-arrow { border-left: 17px solid none;"
                       "border-right: 17px solid none; border-bottom: 17px solid black; width: 0px; height: 0px; }"
                       "QSpinBox::up-arrow:hover { border-left: 17px solid none;"
                       "border-right: 17px solid none; border-bottom: 17px solid black; width: 0px; height: 0px; }"
                       "QSpinBox::up-button { width: 80px; height: 77px; background-color: yellow; }"
                       "QSpinBox::up-button:hover { width: 80px; height: 77px; background-color: yellow; }"

                       "QSpinBox::down-arrow { border-left: 17px solid none;"
                       "border-right: 17px solid none; border-top: 17px solid black; width: 0px; height: 0px; }"
                       "QSpinBox::down-arrow:hover { border-left: 17px solid none;"
                       "border-right: 17px solid none; border-top: 17px solid black; width: 0px; height: 0px; }"
                       "QSpinBox::down-button { width: 80px; height: 77px; background-color: yellow; }"
                       "QSpinBox::down-button:hover { width: 80px; height: 77px; background-color: yellow; }"
    );

}

Any help?

3个回答

5

首先,这个样式表代码无法工作。一些行缺少结束符“}”。

如果要设置QSpinBox的最小尺寸,请尝试以下方法:QSpinBox { border: 3px solid red; border-radius: 5px; background-color: yellow; min-width: 200px; min-height: 200px; }

使用此方法,我得到了具有以下样式的大而丑陋的旋转框:

"QSpinBox { border: 3px solid red; border-radius: 5px; background-color: yellow; min-height: 150px; min-width: 150px; }"
"QSpinBox::up-arrow { border-left: 17px solid none;"
"border-right: 17px solid none; border-bottom: 17px solid black; width: 0px; height: 0px; }"
"QSpinBox::up-arrow:hover { border-left: 17px solid none; "
"border-right: 17px solid none; border-bottom: 17px solid black; width: 0px; height: 0px; }"
"QSpinBox::up-button { min-width: 80px; min-height: 77px; background-color: yellow; }"
"QSpinBox::up-button:hover { min-width: 80px; min-height: 77px; background-color: yellow; }"
"QSpinBox::down-arrow { border-left: 17px solid none;"
"border-right: 17px solid none; border-top: 17px solid black; width: 0px; height: 0px; }"


"QSpinBox::down-arrow:hover { border-left: 17px solid none;"
"border-right: 17px solid none; border-top: 17px solid black; width: 0px; height: 0px; }"
"QSpinBox::down-button { min-width: 80px; min-height: 77px; background-color: yellow; }"
"QSpinBox::down-button:hover { min-width: 80px; min-height: 77px; background-color: yellow; }"

enter image description here


它对我来说不起作用,看起来像默认设置。可能是什么原因? - Roman Rdgz
嗯,在你的代码中,你引用了 QDoubleSpinBox 但是却样式化了 QSpinBox。这可能是一个差异,我猜测。 - Pucor

0

抱歉,我来晚了。看看这个。我意识到的是,在下拉按钮和上拉按钮上都必须设置'subcontrol-origin: ...'和'subcontrol-position: ...'。我正在使用python+pyqt。希望能对你有所帮助。


0
在样式表字符串的最后一行加上分号。
使用


"QSpinBox::down-button:hover { width: 80px; height: 77px; background-color: yellow; };"

而不是

"QSpinBox::down-button:hover { width: 80px; height: 77px; background-color: yellow; }"

那么您可能会稍后更改样式表。 - ScarCode
我不是。GUI 是使用 Qt Creator 设计的,所以我能想到的唯一可能就是某些属性未启用,这样更改就无法正确应用。 - Roman Rdgz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接