PDA

View Full Version : Q_OBJECT and CSS background-image



nelbok
20th January 2011, 11:31
Hello,

I have a problem with Q_OBJECT and background image.



#ifndef MYCLASS_HH
# define MYCLASS_HH

# include <QWidget>

class myclass: public QWidget
{

public:
myclass(QWidget* parent = 0);

Q_OBJECT
};

#endif // MYCLASS_HH

#include "myclass.hh"

myclass::myclass(QWidget* parent)
: QWidget(parent)
{
setStyleSheet("background-image: url(../QSS/background.png)");
}



When i put in comment Q_OBJECT macro, i see my background image on my widget.
But when Q_OBJECT macro is not commented, i don't see my background image.

Why ?

I have test this code on my widget :


QPalette palette;
QPixmap pixmap = QPixmap("../QSS/background.jpg");
palette.setBrush((this)->backgroundRole(), QBrush(pixmap));
(this)->setPalette(palette);


It works, bug i don't see how i can use a CSS external file with this...

Thanks for you help.

wysota
20th January 2011, 11:50
Q_OBJECT has nothing to do with CSS. Make sure you rebuild everything properly and change your relative path to an absolute one to make sure it is not influencing the outcome.

nelbok
20th January 2011, 12:01
Hi wysota,

I have rebuild everything, clear build dir for all tests.
I have change relative path to an absolute one.

I have test on Windows, Linux (Fedora 14 x86_64) with Qt version 4.6.3, 4.7.0 and 4.7.1.

But i don't see my background image on my widget :(

wysota
20th January 2011, 12:04
Please provide a minimal compilable example reproducing the problem.

nelbok
20th January 2011, 12:19
Done.

if you put in comment Q_OBJECT macro, it works.

wysota
20th January 2011, 12:32
If you want custom QWidget subclasses to support stylesheets, you need to provide the following code:

void myclass::paintEvent(QPaintEvent *pe) {
QStyleOption o;
o.initFrom(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
};

nelbok
20th January 2011, 12:52
Thanks a lot, it works perfectly now. :)