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. Set color for each index in a Qlist of type color
QtWS25 Last Chance

Set color for each index in a Qlist of type color

Scheduled Pinned Locked Moved General and Desktop
qlistqcolor
7 Posts 2 Posters 3.6k 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.
  • RatzzR Offline
    RatzzR Offline
    Ratzz
    wrote on last edited by Ratzz
    #1

    I want to have a QList of type color and set the color for each index of a Qlist

    QList<QColor> colorlist;
    QColor color;
    for(int i=0;i<10;i++)
    colorlist.at(i)=color.setRgb(0,102,104,255);
    

    How to set color for each index ? I have tried this link.
    nor this worked

    colorlist.at[i]=color.setRgb(0,102,104,255);
    

    what is the right way to do it ?

    --Alles ist gut.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      I have tried this link

      Have you? The answer clearly says not to use at(), as it returns a const reference.

      QList<QColor> colorlist = ...;
      for(int i=0;i<10;++i)
         colorlist[i].setRgb(0,102,104,255);
      

      The list needs to have 10 elements already or the above will crash trying to modify an element that is not there.
      If it's empty and you want to put 10 new elements then

      QList<QColor> colorlist;
      for(int i=0;i<10;++i)
         colorlist << QColor::fromRgb(0,102,104,255);
      
      1 Reply Last reply
      0
      • RatzzR Offline
        RatzzR Offline
        Ratzz
        wrote on last edited by Ratzz
        #3

        @Chris-Kawa
        Thanks for the reply.
        Even This crashes

            QList<QColor> colorlist;
            colorlist << QColor::fromRgb(0,102,104,255),QColor::fromRgb(0,0,104,255);
            for(int i=0;i<2;++i)
            colorlist[i].setRgb(0,102,104,255);
        

        How to add new item to colorlist ?What is that i missed?

        --Alles ist gut.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Ratzz said:

          colorlist << QColor::fromRgb(0,102,104,255),QColor::fromRgb(0,0,104,255);

          Should be

          colorlist << QColor::fromRgb(0,102,104,255) << QColor::fromRgb(0,0,104,255);
          

          so << not ,.

          1 Reply Last reply
          1
          • RatzzR Offline
            RatzzR Offline
            Ratzz
            wrote on last edited by Ratzz
            #5

            @Chris-Kawa
            My intention is to set different color to the different items in the combobox .
            My combobox has 5items "item 0" to "item 4"

            int textindex=comboBox->currentIndex();
            for(textindex=0;textindex<colorlist.size();textindex++)//colorlist.size()
            model->setData(index, colorlist[textindex], Qt::BackgroundRole);
            

            My colorlist has

             colorlist << QColor::fromRgb(0,102,104,255)<<QColor::fromRgb(102,102,104,255)<<QColor::fromRgb(0,0,104,255);
             for(int i=0;i<3;++i)
            colorlist[i].setRgb(i*100,255,104,255);
            

            why should we set value to colorlist initially? how many values should this have initially?
            my combobox displays only one color(Black) for all the items. why so?

            --Alles ist gut.

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Sorry but your code makes no sense at all.

              int textindex=comboBox->currentIndex();                           //this sets textindex to some value
              for(textindex=0;textindex<colorlist.size();textindex++)     //this sets it again to other values so what's the point of previous assignment?
              
              colorlist << QColor::fromRgb(0,102,104,255) << ... //thiss adds 3 items to the list
              for(int i=0;i<3;++i)
                  colorlist[i].setRgb(i*100,255,104,255);  //and this changes them to other colors so what's the point of the above?
              

              why should we set value to colorlist initially?

              If you're going to change them then you definitely shouldn't.

              how many values should this have initially?

              I don't know what you mean by initially but the list should obviously have as many items as you want to set.

              So, for example:

              QComboBox* combo = new QComboBox();
              
              QStringList items;
              items << "Foo" << "Bar" << "Bazz";
              combo->addItems(items);
              
              QList<QColor> colors;
              colors << QColor::fromRgb(0,102,104,255) << QColor::fromRgb(102,102,104,255) << QColor::fromRgb(0,0,104,255);
              
              for(int i = 0; i < colors.size(); ++i)
                  combo->setItemData(i, colors[i], Qt::BackgroundRole);
              

              If you're on a c++11 compiler you can even use this nicer initializerlist syntax, e.g. instead of

              QList<QColor> colors;
              colors << QColor::fromRgb(0,102,104,255) << QColor::fromRgb(102,102,104,255) << QColor::fromRgb(0,0,104,255);
              

              you could do

              QList<QColor> colors = { QColor::fromRgb(0,102,104,255), QColor::fromRgb(102,102,104,255), QColor::fromRgb(0,0,104,255) };
              
              1 Reply Last reply
              1
              • RatzzR Offline
                RatzzR Offline
                Ratzz
                wrote on last edited by
                #7

                @Chris-Kawa
                Thanks for the reply
                I messed up as you mentioned above . I used your code to make it work. thank you ;)

                QList<QColor> colors = { QColor::fromRgb(0,102,104,255), QColor::fromRgb(102,102,104,255), QColor::fromRgb(0,0,104,255) };
                for(int i = 0; i < colors.size(); ++i)
                    combo->setItemData(i, colors[i], Qt::BackgroundRole);

                --Alles ist gut.

                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