QListWidgetItem setForeground does not work with global stylesheet
-
Hi all,
I am using Qt 5.15.2 on Windows and I have a problem with foreground color of the listwidgetitem.
This foreground color / text color is applied when I am not using a global stylesheet from a qss file.
But when I am loading the stylesheet at startup the text color of the item cannot be set. The foreground color remains.Is there any way to do this or is this not possible due to the stylesheet?
regards
Oliver -
@stvokr said in QListWidgetItem setForeground does not work with global stylesheet:
This foreground color / text color is applied when I am not using a global stylesheet from a qss file.
But when I am loading the stylesheet at startup the text color of the item cannot be set.Show what you have, that does/does not work (minimal example). I for one do not understand what you are saying here.
-
Hi,
this is a small program which describes the same problem.
#include <QApplication> #include <QtWidgets> #include <QListWidget> int main(int argc, char *argv[]) { int size = 100; const QString stylesheet = "QListView { background-color: black; }" "QListView::item { color: green }"; QApplication a(argc, argv); QMainWindow w; w.setMinimumSize(size, size); w.setMaximumSize(size, size); w.setStyleSheet(stylesheet); QListWidget l(&w); l.setMinimumSize(size, size); l.setMaximumSize(size, size); w.layout()->addWidget(&l); QListWidgetItem item1( "Item 1" ); item1.setForeground(Qt::red); l.addItem(&item1); QListWidgetItem item2( "Item 2" ); item2.setForeground(Qt::blue); l.addItem(&item2); w.show(); return a.exec(); }
The results of this example is this (the text is green as defined in the stylesheet, but the foreground color set by the code is ignored):
When I don't load the stylesheet or don't define the color for the QListView::item, the result looks like this:
I would like to have is both cases combined, so that the listwidgetitems have as default color green, but can be highlighted dynamically and individually. AFAIK the items cannot be colored individually by stylesheet, right?
regards
Oliver -
@stvokr
How do you get those red & blue colors if you do not define anything for the QListView items at all? I don't know if your pic is from Windows, which i don't use, but I do not get that under Linux. Either you have some explicit color somewhere or maybe one of those two items is selected and shows like that --- but I do not think any platform shows a selected list widget item by changing its foreground color, mostly they change the background color to show as selected.Create a new project, no stylesheets. Either create a new list widget in code with some items, or in Designer. Is that red/blue really still there?
-
@stvokr said in QListWidgetItem setForeground does not work with global stylesheet:
The color is set be the setForeground()-function.
But you said
When I don't load the stylesheet or don't define the color for the QListView::item, the result looks like this:
Now it seems that you are setting colors after all. So I at least don't know which you are setting, whther you are setting from code or QSS, and what you want to override what when.
-
Ok, I try to explain.
First case: (green text color)
I set the "color"-property in the stylesheet which is applied to all ListWidgetItems in the application. In the example above only the default color is defined, but no "hover", "selected" or "disabled" color. Then I load the stylesheet in the mainwindow to apply it to all widgets. For this case the setForeground()-function can be called but nothing happens. I think that the stylesheet overrides the color settings.Second case: (colored text)
When I don't load the stylesheet the setForeground()-function is creating the colored items, because there is no stylesheet which overrides the color settings.So in every case I call the setForeground()-function, but with a loaded stylesheet the text color cannot be changed by this function.
Now I am wondering how to change the text color dynamically from code with a loaded stylesheet that defines the basic colors from QSS. This is a combination of both cases above. So I need to override the basic color settings from the stylesheet.
regards
Oliver -
@stvokr
I wrote earlierOne thing: if you are saying you apply a color in code to style, that will be overridden/ignored if there is stylesheet rule applicable. Depends how you set the color of the listwidgetitem.
So, yes, if you use QSS that will override
setForeground()
etc. You will need to stick with one or the other.