Subclass of QWizard; setting background in style sheet causes title bar to be blank.

  • I have class derived from QWizard, and everything is working fine. However, the background color is white. The application I am working on uses an off-white color for the background of much of the UI, so to match that, I tried this:

    wizard.setStyleSheet("background: #f2f1ed;")

    The title bar now comes up blank, with no close button. If I remove the line, I get the close button back.

    Has anybody else met with this and overcome it? I'm fairly new to UI programming on Qt, so there may well be a simple thing I have missed. I am using Qt 5.7.1 msvc2015 on the Windows platform. I can't use 5.9, because the target platform is WinXP.

    Thanks in advance for any help anybody can offer.

  • I should point out that I'm also overriding the paintEvent to do some custom painting. If I don't override it, and I set the style sheet, then the title bar behaves. If I do override it, but don't set the style sheet, then the title bar behaves. It's when I override paintEvent and set the style sheet that the bar is blank.

  • Lifetime Qt Champion

    Do you call the base class paint ?
    Also to use the setting from a stylesheet, you need to use the methods
    in QStyle to draw. else the stylesheet is not in effect.

    I would try to change its palette
    (in widget)
    QPalette pal = palette();
    pal.setColor(QPalette::Background, Qt::black
    Note it might not use QPalette::Background but some other role.

  • @mrjj Thanks for the reply.

    Yes, I do call the base class paintEvent, before I do my own painting. I don't understand why that works without setting the style sheet, but then causes the title bar to disappear when I set the style sheet.

    I'll experiment with your suggestion.

    Thanks again.

  • Lifetime Qt Champion

    Stylesheets are a bit all or nothing
    So when you give it a sheet, some things is no longer drawn.
    So often some images falls off etc. You have to include the styling for those also
    to keep them etc.

    That is why i suggest trying the palette if all you want is change bg color.
    a fast test to see if it will work is just loop over all the roles and set all to say Qt:red

     QPalette bgpal = palette();
        for (int cc=0; cc < QPalette::NColorRoles; cc++ )
        bgpal.setColor (cc, Qt::red);

  • Even setting every color in the palette to the right one doesn't get the background color right. I think I'll just pass on this for now - I don't have the time to fight with this.

    Thanks very much for your help.

  • Lifetime Qt Champion

    np, i have not self painted a QWizard
    so not sure if it even does paint background or you see something else "behind it"
    Widgets (the actual class) do not have
    on pr default.

    Can you post your paint ?
    (even if u skip it for now :)

  • Sure, here's what's in my paintEvent override:

        // Paint the banner.
        QPainter painter(this);
        const int x{style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + BannerFudge};
        const int y{style()->pixelMetric(QStyle::PM_TitleBarHeight)};
        painter.drawImage(x, y, m_banner);

    BannerFudge is just a number to line it up right (I'm probably using the wrong PM_* enum to find the width of the edge of the window) and m_banner is of type QImage.

    Thanks for continuing to follow this!

  • Lifetime Qt Champion

    @Paul-Thompson said in Subclass of QWizard; setting background in style sheet causes title bar to be blank.:


    I wonder what you really see if from
    void QWizardHeader::paintEvent(QPaintEvent * /* event */)

    If we look at the base drawing, unless
    IsVistaThemeEnabled(QVistaHelper::VistaBasic) is true then
    im not sure it clear bg at all.

    void QWizard::paintEvent(QPaintEvent * event)
        if (d->wizStyle == MacStyle && currentPage()) {
            QPixmap backgroundPixmap = currentPage()->pixmap(BackgroundPixmap);
            if (backgroundPixmap.isNull())
            QPainter painter(this);
            painter.drawPixmap(0, (height() - backgroundPixmap.height()) / 2, backgroundPixmap);
    #if QT_CONFIG(style_windowsvista)
        else if (d->isVistaThemeEnabled()) {
            if (d->isVistaThemeEnabled(QVistaHelper::VistaBasic)) {
                QPainter painter(this);
                QColor color = d->vistaHelper->basicWindowFrameColor();
                painter.fillRect(0, 0, width(), QVistaHelper::topOffset(), color);
    it calls 
    void QVistaHelper::paintEvent(QPaintEvent *event)
    	    QPainter painter(wizard);
    in drawTitleBar, it only clear if
    if (vistaState() == VistaAero && isWindow)
    	        drawBlackRect(QRect(0, 0, wizard->width(),
                                titleBarSize() + topOffset()), hdc);	

    So i have this feeling you are seeing through it and its really another widgets background we are after.
    Namely the pages own.
    Since stylesheets are cascading, setting on the Dialog would also affect pages.

  • Thanks - I appreciate the effort, and I'll look into that. I'm setting the background in the pages' style sheets, but I haven't done anything like the experimentation with that that I did with the wizard itself. I'll come to it tomorrow.

    Again, thanks for spending the time.

  • Lifetime Qt Champion

    np, i have a hidden agenda as i soon will use the qwizard for a project
    and i also have other base bg color :)

    Would it be possible to show image
    (use and post link)
    To show the actual area that is not getting colored.
    Just so im 100% sure what you mean as if you set style on the pages
    it cant be in that area you mean.

  • @mrjj

    Here you go:

    I've blocked out in red the bits you don't need to see. The green bar is the banner graphic. The dark blue section is the background color as set by the QWizardPages. There are no navigation buttons, as I use custom ones which are in the red bit at the bottom. All the white I can't get to. The top section is the title bar (no text, which is by design). I can't paint onto the other white bits as they are clipped out. It's those bits I need to be the background color. If possible, I'd also like to paint onto them, but I am guessing that is impossible.

    If you can't see the different colors, let me know and I can number the sections or something.

  • Lifetime Qt Champion

    alt text

    Those areas i assume.

    Im pretty sure its not the widget we expect, else your coloring should "hit" it.

    Using this

    should be much like your?
    Also you did not alter margins or anything like that with stylesheets for the pages?

  • Lifetime Qt Champion

    Seems to be related to what QStyle used.
    if i change that with

    if u use the test project

    and add before .exec()

      QPalette bgpal = wizard.palette();
      for (int color = 0; color < QPalette::NColorRoles; color++ ) // just lazy. should use correct roles
        bgpal.setColor ((QPalette::ColorRole)color, Qt::red);
      const QObjectList& list = wizard.children();
      for (int cc = 0; cc < list.size(); cc++) {
        QWidget* widget = qobject_cast<QWidget*>(list[cc]);
        if (!widget) continue; // skip all non widgets
      return app.exec();

    alt text

    Using just
    wizard.setStyleSheet("background-color: rgb(255, 255, 0);");

    i get
    alt text

    So it seems to depend on the QStyle used and how much is used from palette and
    stylesheet to draw the dialog.

    Your layout of the pages (i have no side margin) seems different so might be other case for you.
    Maybe even due to using styles on the pages.

  • Thanks for the research. I tried your code and got some slightly different results, even when removing the style sheet from the pages.

    Using ModernStyle gets the white area which wasn't previously gray to turn gray. However, I lose my custom-painted banner! I've had zero luck using setPixmap to try to get Qt to put in the banner.

    I think I'm going to live with it for now, as the cosmetic issues here are, frankly, low priority. I think, if I had the luxury of time, I'd implement my own wizard dialog, giving more control to the wizard and its pages. The standard QWizard is great for putting together wizards quickly if you're not too worried about what and where you can paint. My company desires a bit more of a fancy user experience, and QWizard doesn't seem to make that easy.

    I really appreciate the time you've taken, and I hope you don't run into the same issues I did when you undertake your project.

Log in to reply