tags:

views:

131

answers:

1

Hi

I have a class that inherits QStandardItem and I put the elements in a QTreeWidget. The class receive notifications from the outside and I want to change the background color of the item based on what happened.

If I do not use stylesheets it works just fine, like this:

void myClass::onExternalEvent() { setBackground(0, QColor(255,0,0))); }

However as soon as I put a stylesheet on the QTreeWidget this has no effect, the stylesheet seems to override the setBackground call.

So I tried : void myClass::onExternalEvent() { this->setStyleSheet("background-color: red"); }

but this is probably all wrong, it changed the color of some other element on my screen, not sure why.

So anyone have an idea on how I can alter the background color like with setBackgroundColor but still be able to use stylesheet on my QTreeWidget?

A: 

Palettes propagate to the children of a widget, and it's bad to mix and match style-sheet controls and native controls (I do not have a citation for the latter handy, but I have read it in the QT docs somewhere).

That being said, try setting setAutoFillBackground(false) on your QStandardItem derived class.

EDIT: Sorry - also, are you specifying the QTreeWidget in the stylesheet or just setting "background-color:"? If you specify the QTreeWidget only in the stylesheet that might take care of it as well.

QTreeWidget { background-color: white; }

But I think you still have to set the autoFillBackground(false).

Brian Roach
Thanks for the reply, I did not get any different result with setAutoFillBackground. The doc says it will be turned off anyway if there is a stylesheet:http://doc.qt.nokia.com/4.6/qwidget.html#autoFillBackground-propThe thing is that I do not want to change the background for the full QTreeWidget, only for a specific item (a row) inside the widget. My class is a QTreeWidgetItem and I would like to change the color of this item only.
Johan