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

How to reset QApplication::styleSheet for certain buttons in application?

本文关键字:QApplication 按钮 styleSheet 应用程序      更新时间:2023-10-16

在我的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 的按钮才会具有自定义外观,其他按钮看起来正常。