2

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

Screenshot

I have tried to recursively browse all the widgets and set their border to none, but no widget seems to have this border.

Here is my code (the complete buildable example can be found here):

licensewizard.h

#ifndef LICENSEWIZARD_H
#define LICENSEWIZARD_H

#include <QWizard>

class LicenseWizard : public QWizard
{
  Q_OBJECT

public:
  enum
  {
    Page_Welcome
  };
  LicenseWizard(QWidget *parent = 0);
};

class WelcomePage : public QWizardPage
{
  Q_OBJECT

public:
  WelcomePage(QWidget *parent = 0);
};

#endif

licensewizard.cpp

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

#include <QtDebug>

LicenseWizard::LicenseWizard(QWidget *parent)
    : QWizard(parent)
{
    setPage(Page_Welcome, new WelcomePage);
    setStartId(Page_Welcome);
    setWizardStyle(ModernStyle);
    setWindowTitle(tr("License Wizard"));

    for (auto *widget : this->findChildren<QWidget *>())
    {
        widget->setStyleSheet("background:none; border:none; margin:0; padding:0;");
    }
}

WelcomePage::WelcomePage(QWidget *parent)
    : QWizardPage(parent)
{
    setTitle(tr("Welcome"));
}

Is it possible and how?

scopchanov
  • 7,966
  • 10
  • 40
  • 68
matt
  • 384
  • 2
  • 11
  • 1
    A minimal example would be less than half the number of lines, all in one .cpp file, with Java-style classes (no separate method definitions) – and would nicely fit into the question, possibly without scrollbars :). I suggest you do that, it’ll make it easier to reproduce – SO questions aren’t enterprise monstrosities where you need lots of boilerplate. – Kuba hasn't forgotten Monica Sep 27 '18 at 14:16

1 Answers1

2

Cause

This ruler, QWizardRuler *bottomRuler, is not affected by the stylesheet, because QWizardRuler inherits QWizardHeader and the line is drawn in the QWizardHeader::paintEvent:

void QWizardHeader::paintEvent(QPaintEvent * /* event */)
{
    QPainter painter(this);
    painter.drawPixmap(0, 0, bannerPixmap);
    int x = width() - 2;
    int y = height() - 2;
    const QPalette &pal = palette();
    painter.setPen(pal.mid().color());
    painter.drawLine(0, y, x, y);
    painter.setPen(pal.base().color());
    painter.drawPoint(x + 1, y);
    painter.drawLine(0, y + 1, x + 1, y + 1);
}

Solution

Since this ruler could not be removed, I would suggest you to hide it.

The implementation of QWizardHeader::paintEvent gives an idea of how to do that, i.e. by setting the color role used to paint the line, QPalette::Mid, to the appropriate color, which blends with the background, QPalette::Base.

Note: If this color role is used by any other item, its color would be affected as well.

Example

Here is an example I have prepared for you of how the proposed solution could be implemented:

Substitute

for (auto *widget : this->findChildren<QWidget *>())
{
    widget->setStyleSheet("background:none; border:none; margin:0; padding:0;");
}

with

QPalette p(palette());

p.setColor(QPalette::Mid, p.color(QPalette::Base));

setPalette(p);

Result

The given example produces the following result:

QWizard page without a horizontal ruler

scopchanov
  • 7,966
  • 10
  • 40
  • 68
  • Thanks for this usefull answer. It works good in the minimal example, but I don't understand why it does not work in my original code. I tried to disable all stylesheets but it does not change anything. There is still a very thin line. Is there anything else I have to check? – matt Sep 28 '18 at 06:42
  • 1
    @matt, as written in the __Cause__, this line is not affected, I.e. cannot be removed, by setting a Stylesheet, as drawing it is hard coded in the paint event handler. That means, whatever Stylesheet you use, you cannot remove it. But you could hide it modifying the widget’s color palette. – scopchanov Sep 28 '18 at 07:01
  • @matt, since it works, you can mark the answer as accepted. Also, if you like it, you might consider upvoting it, once you reach 15 reputation or more. – scopchanov Sep 28 '18 at 07:04
  • 1
    Yes I understand. Modifying the color palette has no effect in my original code :( But the answer is accepted with the given example. – matt Sep 28 '18 at 07:11
  • @what do you mean by _original code_? I have used your code to create the example. – scopchanov Sep 28 '18 at 07:38
  • I made a minimal example to show my problem. But in my original application, using your fix does not resolve my problem. I don't understand why. – matt Sep 28 '18 at 07:44
  • @matt, then it is minimal, but not complete, as it does not allow your issue to be reproduced. My suggestion is: leave this question as it is. It has already been answered for the generic case. Post a new question, with a link to this one. In the new question, try to be closer to your _specific use case_ by providing enough details for anyone to be able to reproduce your problem. If you haven't done that already, you could also read about [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). I would be more than happy to help you find a solution. – scopchanov Sep 28 '18 at 10:13
  • I found adding a style just after QApplication breaks the solution you gave me. `QApplication app(argc, argv); qApp->setStyleSheet("QLabel {color:black;}"); // or any other styles... ` I don't know why. – matt Oct 05 '18 at 13:06
  • Related question posted here: https://stackoverflow.com/q/52667937/8570451 – matt Oct 05 '18 at 14:33