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. [Solved]QTableview diffrent color row
Forum Update on Monday, May 27th 2025

[Solved]QTableview diffrent color row

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 23.5k 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.
  • T Offline
    T Offline
    toho71
    wrote on last edited by
    #1

    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;

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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]].

      1 Reply Last reply
      0
      • T Offline
        T Offline
        toho71
        wrote on last edited by
        #3

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

        Perfect!!!

        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++)
        {
        
            ui->label1->setText(selectedList.at(i).data().toString());
            ui->label2->setText(dataModel->data(index).toString());
        }
        
        QStandardItem *item = dataModel->itemFromIndex(index);
        ui->label3->setText(item->data(0).toString());
        ui->label4->setText(QString::number(index.row()));
        
        QBrush yellow;
        yellow.setColor(Qt::yellow);
        
        foreach (index, selectedList)
        {
            //dataModel->setData(index, yellow, Qt::BackgroundColorRole);
            dataModel->setData(index, yellow, Qt::ForegroundRole);
            dataModel->setData(index, "pressed");
        }
        ui->testTable->setFocus();
        ui->testTable->selectRow(index.row());
        

        }@

        Anymore hints

        1 Reply Last reply
        0
        • J Offline
          J Offline
          justDance
          wrote on last edited by
          #4

          Hi Andre,

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

          1 Reply Last reply
          0
          • R Offline
            R Offline
            ramasamy.eeegmail.com
            wrote on last edited by
            #5

            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;
            yellow.setColor(Qt::red);
            ui->tableView->model()->setData(index,yellow,Qt::ForegroundRole);
            ui->tableView->model()->setData(index,"Pressed");
            

            }
            @

            Thanks & Regards,
            RAM :)

            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