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. How to change the Text Color of a List Item ?
QtWS25 Last Chance

How to change the Text Color of a List Item ?

Scheduled Pinned Locked Moved Solved General and Desktop
qbrushqcolortext colorlist
6 Posts 3 Posters 5.2k 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
    Sphinkie
    wrote on 3 Jul 2020, 17:09 last edited by Sphinkie 7 Mar 2020, 17:31
    #1

    Hello

    I am trying to change the color of a text in a ListView.
    I can change the text color of a Qlabel, for instance, not for a item in a list...

    Currenltly, my code is the following:

    QListWidgetItem* w = ui->w_MessageList->item(2);
    
    w->setBackgroundColor(QColorConstants::Svg::plum);   // This change background color. It is deprecated but it works.
    
    // Attempt to change the foreground (text) color
    QBrush b = w->foreground().style();       // get the brush of the text
    b.setColor(QColorConstants::Svg::pink);   // change the brush
    w->setForeground(b);                      // apply the brush
    

    But the text is still black.
    Can you help on how to use the Qbrush (or the Qstyle) ?

    Thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 3 Jul 2020, 18:18 last edited by
      #2

      Hi and welcome to devnet,

      What version of Qt ?
      What OS ?
      Can you provide a minimal compilable example that shows that behavior ?

      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 Offline
        S Offline
        Sphinkie
        wrote on 7 Jul 2020, 07:10 last edited by Sphinkie 7 Jul 2020, 07:52
        #3

        Hello @SGaist ,
        Here is the minimal compilable code (Qt 5.15 under Windows):
        With this program, I create a list and add 3 items.
        Then, I try to change the ForegroundColor of an item, to make the text appear Red, but all the texts of the 3 items remain black.
        I don't undertand why.
        Thanks for your help.

        #include <QApplication>
        #include <QListWidget>
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            QWidget fenetre;
            fenetre.setFixedSize(320, 320);
        
            QListWidget w_MessageList(&fenetre);
        
            w_MessageList.addItem("first list item");
            w_MessageList.addItem("second list item");
            w_MessageList.addItem("third list item");
        
            // Attempt to change the color of the text of the second item.
            QListWidgetItem* w = w_MessageList.item(1);
            QBrush b = w->foreground().style();      // get the brush
            b.setColor(QColorConstants::Red);        // change the brush
            w->setForeground(b);                     // apply the brush
        
        
            // w->setBackgroundColor(QColorConstants::Yellow);  // this change the background color (deprecated by Qt)
        
            fenetre.show();
            return a.exec();
        }
        
        B 1 Reply Last reply 7 Jul 2020, 08:06
        0
        • S Sphinkie
          7 Jul 2020, 07:10

          Hello @SGaist ,
          Here is the minimal compilable code (Qt 5.15 under Windows):
          With this program, I create a list and add 3 items.
          Then, I try to change the ForegroundColor of an item, to make the text appear Red, but all the texts of the 3 items remain black.
          I don't undertand why.
          Thanks for your help.

          #include <QApplication>
          #include <QListWidget>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
          
              QWidget fenetre;
              fenetre.setFixedSize(320, 320);
          
              QListWidget w_MessageList(&fenetre);
          
              w_MessageList.addItem("first list item");
              w_MessageList.addItem("second list item");
              w_MessageList.addItem("third list item");
          
              // Attempt to change the color of the text of the second item.
              QListWidgetItem* w = w_MessageList.item(1);
              QBrush b = w->foreground().style();      // get the brush
              b.setColor(QColorConstants::Red);        // change the brush
              w->setForeground(b);                     // apply the brush
          
          
              // w->setBackgroundColor(QColorConstants::Yellow);  // this change the background color (deprecated by Qt)
          
              fenetre.show();
              return a.exec();
          }
          
          B Offline
          B Offline
          Bonnie
          wrote on 7 Jul 2020, 08:06 last edited by Bonnie 7 Jul 2020, 08:07
          #4

          @Sphinkie Hello
          I'm not sure what is your QColorConstants::Red.
          But I've tested your code withQt::red and it works, the second item's text is red.

          And setBackgroundColor is deprecated because since 4.2 we should use setBackground.
          Also, you could directly pass a QColor object as a QBrush parameter.

          w->setForeground(QColor(Qt::red));
          w->setBackground(QColor(Qt::yellow));
          
          1 Reply Last reply
          2
          • S Offline
            S Offline
            Sphinkie
            wrote on 7 Jul 2020, 09:36 last edited by Sphinkie 7 Jul 2020, 09:39
            #5

            @Bonnie said :

            w->setForeground(QColor(Qt::red));
            w->setBackground(QColor(Qt::yellow));

            Thank you. This works very well.

            (and also:

            w->setForeground(QColor(QColorConstants::Red));
            w->setBackground(QColor(QColorConstants::Yellow));
            

            Colors are from https://doc.qt.io/qt-5/qcolorconstants.html

            B 1 Reply Last reply 7 Jul 2020, 09:58
            0
            • S Sphinkie
              7 Jul 2020, 09:36

              @Bonnie said :

              w->setForeground(QColor(Qt::red));
              w->setBackground(QColor(Qt::yellow));

              Thank you. This works very well.

              (and also:

              w->setForeground(QColor(QColorConstants::Red));
              w->setBackground(QColor(QColorConstants::Yellow));
              

              Colors are from https://doc.qt.io/qt-5/qcolorconstants.html

              B Offline
              B Offline
              Bonnie
              wrote on 7 Jul 2020, 09:58 last edited by Bonnie 7 Jul 2020, 11:41
              #6

              @Sphinkie
              Wow, I had not noticed there is such a namespace since I'm still mainly using 5.12.
              If the constants are QColor objects as the doc says, then you should be able to use them directly like

              w->setForeground(QColorConstants::Red);
              w->setBackground(QColorConstants::Yellow);
              
              1 Reply Last reply
              0

              5/6

              7 Jul 2020, 09:36

              • Login

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