c++ - 如何为应用程序中的某些按钮重置 QApplication::styleSheet?

标签 c++ qt qt5 qtstylesheets

在我的 main() 函数中,我在应用程序的所有按钮上设置样式表。

qApp->setStyleSheet("QPushButton {"
                            "     border: 1px solid #8f8f91;"
                            "     border-radius: 4px;"
                            "     background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #f6f7fa, stop: 1 #dadbde);"
                            "     padding: 3px;"
                            " }");

但是现在我需要重新设置一些按钮的样式。我试过 button->setStyleSheet(""),但它不起作用。那么,该怎么做呢?

最佳答案

你不能。

您可以做相反的事情,即为某些按钮设置样式表,如下所示:

qApp->setStyleSheet("QPushButton[specialButton=\"true\"] {"
                    "     border: 1px solid #8f8f91;"
                    "     border-radius: 4px;"
                    "     background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #f6f7fa, stop: 1 #dadbde);"
                    "     padding: 3px;"
                    "}");

然后:

button->setProperty("specialButton", true);

这样只有 specialButton 设置为 true 的按钮才会有自定义外观,其他按钮看起来正常。

关于c++ - 如何为应用程序中的某些按钮重置 QApplication::styleSheet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51723304/

相关文章:

C++:将派生类保存在基类的shared_ptr中

c++ - 如何强制编译器选择特定的函数特化

qt - 如何使用 QWebElement 设置输入值(类型 ="file")?

c++ - Qt 可检查操作

qt - QML 如何获取 ListView 中的当前元素索引?

c++ - 每个解决方案一个版本的 nuget 包(但不是最新的)

c++ - 在已经是模板可变参数的函数中传递可变参数

c++ - 如何使用 QTestLib 等待 QWidget 关闭? [Qt5]

c++ - 在Qt中从父节点中查找子节点

c++ - Qt 5.4/Qml : Prevent binding loop