0
votes

I created a list view model by inheriting from QAbstractListModel. I implemented data(const QModelIndex &, int ) to provide the list item background colour (on Qt::BackgroundRole) and item text color (on Qt::ForegroundRole).

It works when I run my application without a stylesheet, but as soon as I apply a stylesheet (using QApplication::setStyleSheet) the text color ends up being wrong. The text color stays the color set in the stylesheet.

I debugged through the model and even though it is returning, for example QBrush(QColor(255, 0, 0)), on the ForegroundRole it never applies to the list view.

Is there something I need to do extra to get my list view model to work correctly when a stylesheet is set for the application?

Correct Answer (from answer by king_nak) [https://stackoverflow.com/a/41673494/1151329]:

I added the following in the style sheet (.qss file) after the original QListView block

QListView[default_style="true"]
{
    color : default;
}

In my code I then called

ui.listView->setProperty("default_style", QVariant(true));

It works well with any other QListView being styled by the style sheet, but this one with the 'default_style' property uses the palette set by the widget.

1
Do you explicitly set colors for the list widget in your stylesheet? This would of course override the model's hints - king_nak
The stylesheet has properties for list view and list widget, but I also have other lists in the app which should use this style. I'm looking for a way to build a list model that can override the set style -- which is what I expected to happen. - Arno Duvenhage

1 Answers

1
votes

You want to reset the text color of the stylesheet for your specific list widget. To do this, you extend your stylesheet to user default in a separate selector for that widget.

E.g.:

QListView {
    /* Style common to all list views */
}

QListView#MySpecialList {
    color: default;
    /* Other adjustments */
}

In this case, the Object name of your list view must be MySpecialList. Optionally, you can also add a dynamic property and test for it in the stylesheet:

QListView[specialList="true"] { /* ... */ }