2

I have very simple stylesheet that is applied on the application level (QApplication->setStylesheet) before first window is created, but the #LeftSidebar css rule is not working and the backgound color is not affected. On the opposite the #Editor rule works as expected.

What i did wrong? Sidebar is just QWidget subclass that has one QPushButton.

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->resize(1200, 768);
    QSplitter* splitter = new QSplitter();
    splitter->setObjectName("MainSplitter");
    splitter->setChildrenCollapsible(false);

    Sidebar* leftSidebar = new Sidebar();
    leftSidebar->setObjectName("LeftSidebar");
    splitter->addWidget(leftSidebar);

    QTextEdit* editor = new QTextEdit();
    editor->setObjectName("Editor");
    splitter->addWidget(editor);

    QWidget* central = new QWidget();

    QVBoxLayout* layout = new QVBoxLayout();
    layout->setMargin(0);    
    central->setLayout(layout);

    Header* header = new Header();
    header->setObjectName("Header");

    layout->addWidget(header);
    layout->addWidget(splitter);
    this->setCentralWidget(central);
}

application.css

#LeftSidebar {
    background-color: rgb(0,0,0);
}

#Editor {
    background-color: rgb(0,0,0);
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Krab
  • 6,526
  • 6
  • 41
  • 78

2 Answers2

3

There are two good answers in Qt Stylesheet for custom widget:

If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:

void CustomWidget::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
Community
  • 1
  • 1
Orest Hera
  • 6,706
  • 2
  • 21
  • 35
  • This is not an answer. First: it is not necessary to subclass `QFrame` to customize border / background. Second: question is about qss, but not about workarounds. – Dmitry Sazonov Oct 28 '15 at 08:54
  • @SaZ It is not a workaround. I cite Qt documentation that clearly states if you want to use stylesheet with subclass of `QWidget` you must provide `paintEvent`. – Orest Hera Oct 28 '15 at 09:31
  • @OrestHera author is not asking about documentation. There are everything OK with selectors/custom widgets. Anyway, your suggestion about subclassing `QFrame` is wrong. So you should fix or remove your answer. I already provided a correct one in comments. – Dmitry Sazonov Oct 28 '15 at 09:41
  • @SaZ Your answer is not correct. Setting border does not fix the issue. The correct answer if you directly subclass from `QWidget` you should provide `paintEvent` as documented. In that case qss works as expected. – Orest Hera Oct 28 '15 at 09:43
  • @OrestHera you don't need to implement `paintEvent` at all to customize background of any widget. Default implementation wokrs. Author say nothing about custom paint event. His `Sidebar` may be a simple container. – Dmitry Sazonov Oct 28 '15 at 09:51
  • @SaZ "Sidebar is just QWidget subclass". Try it. I tried that with `Sidebar::paintEvent()` it works, but without it does not work even if you provide border. – Orest Hera Oct 28 '15 at 09:53
  • @OrestHera agree, I have similar problems, but with native API inside `QStyle`. For example, custom color for `QPushButton` will not work with Vista style. – Dmitry Sazonov Oct 28 '15 at 10:00
0

You might forget trim the style sheet text. if not, it will read only first line. Example:

QTextStream in(&css_file);
QString strCss = in.readAll();
strCss.trimmed();
setStyleSheet(strCss);
A.J
  • 338
  • 1
  • 4
  • 16