3

I am working on stylesheet of a QWizard and I would like to remove the horizontal line just above the push button.

I've already posted a minimal example here, the question was solved by scopchanov from the minimal example, but I have some lines of code in my project that avoids the solution to work, so I post another question here.

Screenshot

Here is my code (the complete buildable example can be downloaded from the gist here):

licensewizard.h

#include <QWizard>

class LicenseWizard : public QWizard {
  Q_OBJECT
public:
  LicenseWizard(QWidget *parent = 0);
};

licensewizard.cpp

#include <QApplication>
#include <QtWidgets>
#include "licensewizard.h"

LicenseWizard::LicenseWizard(QWidget *parent) : QWizard(parent) {
    setWizardStyle(ModernStyle);

    // solution from @scopchanov https://stackoverflow.com/a/52541248/8570451
    QPalette p(palette());
    p.setColor(QPalette::Mid, p.color(QPalette::Base));
    setPalette(p);
}

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // this line breaks @scopchanov solution.
    // replace QLabel by QPushButton, or anything else... still broken.
    qApp->setStyleSheet("QLabel { color:black; }");

    LicenseWizard wizard;
    wizard.show();

    return app.exec();
}

As scopchanov said, I used the QPalette trick. But I have a big style sheet defined on the qApp and this is the cause of my problem. Using a very small style give the same problem.

The step to reproduce is to add this line after the declaration of the QApplication:

qApp->setStyleSheet("QLabel { color:black; }");

I hope someone could help me.

scopchanov
  • 7,966
  • 10
  • 40
  • 68
matt
  • 384
  • 2
  • 11

1 Answers1

2

To fix this, set the palette of the whole application, instead of just the LicenseWizard class, like this:

LicenseWizard::LicenseWizard(QWidget *parent) : QWizard(parent) {
    setWizardStyle(ModernStyle);
}

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QPalette p(qApp->palette());

    p.setColor(QPalette::Mid, p.color(QPalette::Base));
    qApp->setPalette(p);
    qApp->setStyleSheet("QLabel { color:black; }");

    LicenseWizard wizard;
    wizard.show();

    return app.exec();
}

Note: As mentioned in my answer to the linked question, if this color role is used by any other item, its color would be affected as well.

scopchanov
  • 7,966
  • 10
  • 40
  • 68
  • Will not that affect other widgets? – eyllanesc Oct 05 '18 at 15:38
  • @eyllanesc, most certainly. I cannot think of other solution, though. In my answer to the linked question I've explicitly mentioned that. – scopchanov Oct 05 '18 at 15:40
  • 1
    So I recommend placing it as a warning in your question. It is always good to indicate the limitations of the answers. – eyllanesc Oct 05 '18 at 15:41
  • 2
    I recommend placing it here, I assure you that very few will review your linked question. – eyllanesc Oct 05 '18 at 15:43
  • 1
    @eyllanesc, I already did. Thank you for the remark! I appreciate it. – scopchanov Oct 05 '18 at 15:43
  • 1
    Using qApp->setStyleSheet, why do I need to define the palette for the whole appication? The line is painted inside of the QWizard. Thanks you again, this is the accepted answer. – matt Oct 05 '18 at 15:49
  • @matt, I know it does not sound logical. I have dug in the Qt sources the whole last night without much success for finding a reasonable explanation. The closest I get is that `QApplication::setStyleSheet` resets the palette [along the way](https://code.woboq.org/qt5/qtbase/src/widgets/kernel/qapplication.cpp.html#_ZN12QApplication8setStyleEP6QStyle) and of course the warning in the [documentation](http://doc.qt.io/qt-5/qapplication.html#setPalette) not to use `QApplication::setPalette` in conjunction with stylesheets. – scopchanov Oct 06 '18 at 11:36