Johan
Johan

Reputation: 21

Qt: Background color on QStandardItem using StyleSheet

I have a class that inherits QStandardItem and I put the elements in a QTreeWidget. The class receives 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.

Does 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?

Upvotes: 2

Views: 3916

Answers (1)

Brian Roach
Brian Roach

Reputation: 76888

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).

Upvotes: 0

Related Questions