为自定义 QT 类应用不同的 QSS 样式表 [英] Apply different QSS stylesheet for custom QT class
问题描述
我的 gui 使用的是 qss 样式表.它运行良好,但我想为我的自定义小部件定义不同的样式表.例如,QPushbutton
的样式效果很好,但我想为 MyQPushButton
应用不同的样式(从 QPushButton 扩展).
I am using a qss stylesheet for my gui. It works well but I would like to define a different style sheet for my custom widget.
By example, QPushbutton
's style works well but I would like to apply a different style for MyQPushButton
(extends from QPushButton).
我尝试过这样的事情:
MyQPushButton
{
color: #dcdcdc;
// some code here
}
QPushButton
{
color: #b1b1b1;
// some code here
}
但是QPushButton
的样式已经应用在MyQPushButton
上.如何覆盖这种行为?
But QPushButton
's style has been applied on MyQPushButton
.
How to override this behavior ?
推荐答案
如果您想为项目中的特定元素设置样式,可以这样使用;
If you want to style a specific element in your project, you can use it like that;
QPushButton#MyQPushButton
{
color: #dcdcdc;
// some code here
}
我假设您已经将新的 QPushButton
对象命名为 MyQPushButton
,您可以像这样命名一个元素;
I assume that, you've already named your new QPushButton
object as MyQPushButton
, you can name an element like this;
QPushButton* myButton = new QPushButton;
myButton->setObjectName("MyQPushButton");
如果不命名元素,就无法访问它以赋予其覆盖样式.
Without naming your element, you can't access it to give it a overrided style.
这篇关于为自定义 QT 类应用不同的 QSS 样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!