Fareanor
Fareanor

Reputation: 6805

Qt Q_OBJECT macro causes unexpected behaviour with style sheets

I have written a custom Qt widget extending the QWidget class.

Let's consider the following code:

.h

#ifndef SS_TEST_H
#define SS_TEST_H

#include <QMainWindow>

class TestWidget : public QWidget
{
    Q_OBJECT // ***>>> BUG HERE <<<***

    public:
        TestWidget(const QString & v1, const QString & v2, QWidget * parent = nullptr);
};

class TestWindow : public QMainWindow
{
    Q_OBJECT

    public:
        TestWindow();
};

#endif // SS_TEST_H

.cpp

#include "ss_test.h"

#include <QGridLayout>
#include <QLabel>
#include <QApplication>

TestWidget::TestWidget(const QString & v1, const QString & v2, QWidget * parent) : QWidget(parent)
{
    QGridLayout * lay = new QGridLayout;

    QLabel * field = new QLabel(v1, this);
    QLabel * value = new QLabel(v2, this);
    value->setMinimumWidth(80);
    value->setAlignment(Qt::AlignCenter);
    value->setStyleSheet("QLabel { background-color: white; border: 1px solid silver; }");

    lay->addWidget(field, 0, 0);
    lay->addWidget(value, 0, 1);

    this->setLayout(lay);

    this->setStyleSheet("QWidget { background-color: red; }");
}

TestWindow::TestWindow()
{
    setWindowTitle("ss test");
    resize(400, 300);

    QWidget * cw = new QWidget;
    QVBoxLayout * cl = new QVBoxLayout;

    TestWidget * tw1 = new TestWidget("Field 1", "Value 1", this);
    TestWidget * tw2 = new TestWidget("Field 2", "Value 2", this);

    cl->addWidget(tw1);
    cl->addWidget(tw2);
    cl->addStretch();

    cw->setLayout(cl);
    this->setCentralWidget(cw);
}

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

    TestWindow tw;
    tw.show();

    return app.exec();
}

The widget that I am talking about is the TestWidget class.

Without the Q_OBJECT macro in the class declaration, I obtain exactly the style I want: enter image description here

But if I add the Q_OBJECT macro at the beginning of the class declaration (as you can see the comments in the header file), it unexpectedly modifies the style of the widget: enter image description here

I do not understand what happens here.

Of course, in my real project, the widget is way more elaborated than it is in this minimal example and necessarily needs the Q_OBJECT macro (in order to use signal/slots mechanisms and qobject_cast).

I would be very grateful if someone can explain me what Q_OBJECT does here and why.

Upvotes: 3

Views: 276

Answers (1)

Aleph0
Aleph0

Reputation: 6084

One has to read the documentation quite carefully to stumble on the right passage.

Your TestWidget class needs to reimplement the paintEvent:

void TestWidget::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

There is also the important note, that you have to define the Q_OBJECT macro.

Warning: Make sure you define the Q_OBJECT macro for your custom widget.

I tried it and the behavior seemingly fit your needs.

A possible explanation for the strange behavior, in case of lacking Q_OBJECT, might be, that qobject_cast<TestWidget*>(widget) would yield nullptr. That might result in a different behavior for the rendered stylesheet.

Upvotes: 3

Related Questions