Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListWidgetItem setForeground does not work with global stylesheet

QListWidgetItem setForeground does not work with global stylesheet

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stvokr
    wrote on last edited by
    #1

    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

    JonBJ 2 Replies Last reply
    0
    • S stvokr

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        It will partly depend on how you defined your stylesheet.

        As @JonB wrote, please show your stylesheet and as well how you use it.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • S stvokr

          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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @stvokr
          One 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.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            stvokr
            wrote on last edited by
            #5

            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):
            f97ed33c-a40d-4166-badd-8dbe63050827-grafik.png

            When I don't load the stylesheet or don't define the color for the QListView::item, the result looks like this:
            778818aa-2760-4c80-b4a8-8ab5dd8ca33f-grafik.png

            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

            JonBJ 1 Reply Last reply
            0
            • S stvokr

              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):
              f97ed33c-a40d-4166-badd-8dbe63050827-grafik.png

              When I don't load the stylesheet or don't define the color for the QListView::item, the result looks like this:
              778818aa-2760-4c80-b4a8-8ab5dd8ca33f-grafik.png

              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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • S Offline
                S Offline
                stvokr
                wrote on last edited by
                #7

                This is on Windows. And no item is clicked, selected or hovered. The color is set be the setForeground()-function. I will also test it on Linux tomorrow.

                JonBJ 1 Reply Last reply
                0
                • S stvokr

                  This is on Windows. And no item is clicked, selected or hovered. The color is set be the setForeground()-function. I will also test it on Linux tomorrow.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    stvokr
                    wrote on last edited by
                    #9

                    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

                    JonBJ 1 Reply Last reply
                    0
                    • S stvokr

                      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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @stvokr
                      I wrote earlier

                      @stvokr

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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved