gpt4 book ai didi

c++ - Qt : How to make a dynamically added item not comply to style sheet set at design time

转载 作者:行者123 更新时间:2023-11-30 04:11:10 26 4
gpt4 key购买 nike

我目前有一个具有以下样式表的 QToolBar

QToolBox::tab {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
border-radius: 3px;
color: darkgray;
width: 70px;
}

QToolButton { /* all types of tool button */
border: 2px solid #8f8f91;
border-radius: 6px;
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #f6f7fa, stop: 1 #dadbde);
}

QToolButton[popupMode="1"] { /* only for MenuButtonPopup */
padding-right: 30px; /* make way for the popup button */
}

QToolButton:pressed {
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #dadbde, stop: 1 #f6f7fa);
}

/* the subcontrols below are used only in the MenuButtonPopup mode */
QToolButton::menu-button {
border: 2px solid gray;
border-top-right-radius: 6px;
border-bottom-right-radius: 6px;
/* 16px width + 4px for border = 20px allocated above */
width: 30px;
}

现在,上述样式表的问题是 QToolButton 标记应用于我添加到 QtoolBar 的每个工具按钮。无论如何限制 tis 设计的应用到我动态添加的某些 QToolButton。样式表在设计时添加到 QToolBar ?

最佳答案

您可以使用实例化 QToolButton 的对象名称。在 QStyleSheet 中,您可以使用 #ObjectName 定位 QtWidget 继承类的特定实例。

来源中的例子:

QWidget *pWidget = new QWidget(parent);
pWidget->setObjectName("myWidget");

qss 中的示例:

QWidget {
background-color: red;
}

#myWidget {
background-color: green;
}

对于对象名称设置为“myWidget”的小部件,背景将为绿色,对于所有其他小部件,背景将为红色。

如果您也动态更新您的样式表,您最终将需要 polish() 您的小部件。为此,请查看 Qt doc : polish()

关于c++ - Qt : How to make a dynamically added item not comply to style sheet set at design time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20299257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com