Set color for each index in a Qlist of type color



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


  • Moderators

    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);
    


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


  • Moderators

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



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


  • Moderators

    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) };
    


  • @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);

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.