Qt Widgets setStyleSheet

时间:2012-06-14 10:15:55

标签: qt qtstylesheets

由于我正在为平板电脑环境进行编程,因此有些小部件太小而无法进行可触摸的交互。我想特别提高QSpinBox按钮。

经过一番搜索,我发现只有这样才能出现setStyleSheet函数,但是后来却没有很好地解释如何使用它。在这个link to Qt reference中,它描述了我应该传递给setStyleSheet函数的内容,但是我不清楚我是否可以使用它的一部分,或者我应该使用它,即使我只想改变一件事。

无论如何,使用它的一部分或整个文本,我的QDoubleSpinBox在应用此设置后看起来是默认的。当我在mi 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; }"
    );

}

任何帮助?

3 个答案:

答案 0 :(得分:3)

首先 - 这个样式表代码不起作用。某些行中缺少关闭“}”。

要设置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

答案 1 :(得分:0)

将分号放在样式表字符串的最后一行。

使用

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

而不是

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

答案 2 :(得分:0)

对不起,我迟到了。 Check this。我意识到这两个分包控来源:......'和'子控制位置:......'必须设置向下按钮和向上按钮。我正在使用python + pyqt。希望它可以帮助你。