PDA

View Full Version : Style sheet not applied by inheritance ?



moijhd
2nd April 2013, 17:11
Hi,

I have a MyWidget object which inherits from QWidget.

The following function is supposed to change the background color of a MyWidget object but it does not work.

Any remarks ?

Thanks.



// Set a color
void MyWidget::setColor(const QColor &color)
{

// Prepare the style
QString style = "background-color: rgb(%1, %2, %3);";

// Set the color
this->setStyleSheet(
style
.arg(color.red())
.arg(color.green())
.arg(color.blue())
);

qDebug() << this->styleSheet();
}


PS : the debug instruction returns the correct style sheet.

wysota
2nd April 2013, 17:47
Please define "does not work".

moijhd
3rd April 2013, 09:52
The background color of the widget does not change.

What I am trying to achieve here is the following : the user select a color (in a QDialogColor for example) and then I want to draw a rectangle with the chosen color. I did not find a appropriate tool. More precisely I would like to have a button instead...

wysota
3rd April 2013, 11:06
If you want to draw a rectangle then reimplement the paint event and use QPainter::drawRect()