Qt World Summit: Submit your Presentation

[Solved]QTableview diffrent color row

  • Hello
    Yes I now that I can set the Alter.....Rowcolor() but after I filld my datamodel with items and later want to set diffrent colors on the Tableview rows is that possible; Up to x different colors.

    I use SimpelitemModel and Qtableview;

    Some answers and Exampels would be nice;

  • You could do something like this on the relevant table cells (assuming that you are using QStandardItemModel, there is no SimpleItemModel that I know of):

    QColor rowColor = Qt::blue; //or some other color definition
    myCellItem->setData(rowColor, Qt::BackgroundRole);

    Note that you could actually use something more complex than a color, you can use a [[doc:QBrush]].

  • This is solved now.
    Your code was perfect.
    I only had to set solidPattern to the QBrush:


    Thank you Andre
    Yes I ment QStandardItemModel. in a hurry when typing

    Next Problem
    Your code works perfectly on Qt::ForegroundRole
    But not in the BackGroundRole Or BackgroundColorRole

    Yes i used a QBrush to try
    See my simpel Code to this.

    @void Tabelview::on_pushButton_clicked()

    QModelIndexList selectedList= ui->testTable->selectionModel()->selectedIndexes();
    QModelIndex index = ui->testTable->currentIndex();
    for( int i=0; i<selectedList.count(); i++)
    QStandardItem *item = dataModel->itemFromIndex(index);
    QBrush yellow;
    foreach (index, selectedList)
        //dataModel->setData(index, yellow, Qt::BackgroundColorRole);
        dataModel->setData(index, yellow, Qt::ForegroundRole);
        dataModel->setData(index, "pressed");


    Anymore hints

  • Hi Andre,

    Thanks for your answer. But how about QAbstractTableModel? thanks.

  • Hi Friends,

    I followed your procedure to change the row color of tableview and it's not working.. suggest some tips please

    @void QWidget::on_pushButton_clicked()
    //QStandardItem *item =
    QModelIndexList selectedList = ui->tableView->selectionModel()->selectedIndexes();
    QModelIndex index = ui->tableView->currentIndex();

    QBrush yellow;


Log in to reply