Important: Please read the Qt Code of Conduct -

Background color of row in QAbstractItemModel

  • Hello,

    A quick question.
    Is there any way to get the background color of each row in QAbstractItemModel?
    Now I'm implementing QTableView and trying to get current background color in data()(eg.BackgroundRole) in the model. I want to change the background and then, resume it later.
    How can I achieve that?

    Many thanks in advance for your help.


  • Qt Champions 2017

    You should implement data() function. Inside the data function, return the appropriate color for the background role.

    Sample inside the data function.

    case Qt::BackgroundRole: 
        QColor col1(0,255,255);
        QVariant var2 = col1;
        return var2;

  • Thanks for an update, @dheerendra .
    I may be misundersting something. Is it possible to READ the current background color of the corresponding row(line)?


  • Qt Champions 2017

    You can store the background color in the model with Qt::background role. Once u r inside the data(), you can read and return the background color.
    Hope this is what you would like to achieve.

Log in to reply