How to use 2 stylesheets in one system?
-
My system has a generic stylesheet that applies to most parts of the system. But there is another stylesheet that should be applied to one specific QMainWindow. I tried to first set the main style after the app is created and then apply the specific stylesheet. But the minor style sheet isn't applied to the window. Is there something wrong in this approach? Thanks!
in my Main.cpp
int main(int argc, char *argv[]) { QApplication app(argc, argv); // apply the major style sheet to the whole app app.setStyle(new MainStyleSheet); // for a specific window, use another style sheet AnotherWindow* window = AnotherWindow::instance(); window->setStyle(new MinorStyle); } -
Hi,
You are talking about style sheets however, it looks like you implemented two custom styles.
What is the exact situation ? -
Hi,
You are talking about style sheets however, it looks like you implemented two custom styles.
What is the exact situation ?@SGaist thanks for the reply! My system has multiple windows. Most windows should be using stylesheet1, but one window should use stylesheet2. I can't apply both stylesheets to the app (by calling app.setStyle) as one will be overwritten by the other. I tried to apply stylesheet1 to the whole app first, and then apply stylesheet2 to one window (as shown in the previous code example). But only stylesheet1 is actually used. In the specific window the stylesheet2 isn't used at all.
-
@SGaist thanks for the reply! My system has multiple windows. Most windows should be using stylesheet1, but one window should use stylesheet2. I can't apply both stylesheets to the app (by calling app.setStyle) as one will be overwritten by the other. I tried to apply stylesheet1 to the whole app first, and then apply stylesheet2 to one window (as shown in the previous code example). But only stylesheet1 is actually used. In the specific window the stylesheet2 isn't used at all.
@Thinium
Hi
You do mean stylesheet right?
as you show setStyle which is used by QStyle objects and is not related to a stylesheet as such.Stylesheet
https://doc.qt.io/qt-5/stylesheet-reference.html
QStyle
https://doc.qt.io/qt-5/qstyle.html -
ah, sorry. I actually meant style. Here are the two styles:
// used by most parts of the system class MainStyle : public QProxyStyle { public : void polish(QWidget *w) {w->setAttribute(Qt::WA_MacSmallSize);} }; // used only by one window class MinorStyle: public QProxyStyle { public: void polish(QPalette &palette) { palette.setColor(QPalette::Window, QColor(53, 53, 53)); palette.setColor(QPalette::WindowText, Qt::white); palette.setColor(QPalette::Disabled, QPalette::WindowText, QColor(127, 127, 127)); palette.setColor(QPalette::Base, QColor(42, 42, 42)); palette.setColor(QPalette::AlternateBase, QColor(66, 66, 66)); palette.setColor(QPalette::ToolTipBase, Qt::white); palette.setColor(QPalette::ToolTipText, QColor(53, 53, 53)); palette.setColor(QPalette::Text, Qt::white); palette.setColor(QPalette::Disabled, QPalette::Text, QColor(127, 127, 127)); palette.setColor(QPalette::Dark, QColor(35, 35, 35)); palette.setColor(QPalette::Shadow, QColor(20, 20, 20)); // there seems to be a bug for the QPushButton background color // https://bugreports.qt.io/browse/QTBUG-35296 palette.setColor(QPalette::Button, Qt::gray); palette.setColor(QPalette::ButtonText, Qt::white); palette.setColor(QPalette::Disabled, QPalette::ButtonText, QColor(127, 127, 127)); palette.setColor(QPalette::BrightText, Qt::white); palette.setColor(QPalette::Link, QColor(42, 130, 218)); palette.setColor(QPalette::Highlight, QColor(42, 130, 218)); palette.setColor(QPalette::Disabled, QPalette::Highlight, QColor(80, 80, 80)); palette.setColor(QPalette::HighlightedText, Qt::white); palette.setColor(QPalette::Disabled, QPalette::HighlightedText, QColor(127, 127, 127)); } }; //in my main.cpp int main(int argc, char *argv[]) { QApplication app(argc, argv); // apply the major style sheet to the whole app app.setStyle(new MainStyle); // for a specific window, use another style sheet AnotherWindow* window = AnotherWindow::instance(); window->setStyle(new MinorStyle); }The Minor style isn't applied to AnotherWindow. I read the document of QWidget:
https://doc.qt.io/qt-5/qwidget.html#setStyleIt says:
Setting a widget's style has no effect on existing or future child widgets.I wonder if this is the reason why I don't see the style applied correctly to the child widgets of the AnotherWindow. If that's the case, is there a way to apply the style exclusively to AnotherWindow and its child widgets?
-
Hi
You have to set the same style on its children too.
You can use findChildren to make it easy.