Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, January 2008
Style sheets in subclasses of QWidget


Message 1 in thread

Hi,

I use Qt 4.3.3 and want to use the setStyleSheet() method of QWidget to set a custom style sheet for a widget. If the widget is a QWidget, everything works as expected, but if I use my own subclass, the stylesheet is not applied. Consider the following example:

---------- test.cpp
#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>

#include "mywidget.h"

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

	MyWidget * w = new MyWidget();
	w->setStyleSheet("* { background-color: blue; }");

	QMainWindow mw;
	mw.setCentralWidget(w);
	mw.show();

	return app.exec();
}
----------
---------- mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>

class MyWidget : public QWidget {
	Q_OBJECT
public:
	MyWidget(QWidget * parent = NULL);
};

#endif
----------
---------- mywidget.cpp
#include "mywidget.h"

MyWidget::MyWidget(QWidget * parent) : QWidget(parent) {}
----------

If I replace MyWidget with QWidget in test.cpp, the stylesheet is correctly applied. Is this behavior a bug in Qt or do I overlook something?

Thanks,
Henning

--
 [ signature omitted ] 

Message 2 in thread

I guess I found the solution: If QWidget::paintEvent() is 
reimplemented as described in 
http://trolltech.com/developer/task-tracker/index_html?method=entry&id=166742
the example works.

--
 [ signature omitted ]