PDA

View Full Version : Q_OBJECT and styleSheet



babu198649
7th December 2008, 11:14
hi
In the following program the stylesheet works for Widget class only when i comment the Q_OBJECT macro.


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui>

class Widget:public QWidget
{
Q_OBJECT//if this line is commented the setStyleSheet works.

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

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);

private:
Widget *widget;
};

#endif // MAINWINDOW_H


mainwindow.cpp file

#include "mainwindow.h"

Widget::Widget(QWidget *parent) :QWidget(parent)
{
}

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
widget = new Widget();
widget->setStyleSheet("background-color:red");
setCentralWidget(widget);
}

main.cpp file

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

how to set the stylesheet of the Widget Class.

munna
9th December 2008, 06:17
Found this from the docs



QWidget supports only the background, backgorund-clip and background-origin properties.
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);
}
The above code is a no-operation if there is no stylesheet set.
Warning: Make sure you define the Q_OBJECT macro for your custom widget.


May be this can help.