[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:Perfect!!!
Thank you Andre
Yes I ment QStandardItemModel. in a hurry when typingNext Problem
Your code works perfectly on Qt::ForegroundRole
But not in the BackGroundRole Or BackgroundColorRoleYes 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
-
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; yellow.setColor(Qt::red); ui->tableView->model()->setData(index,yellow,Qt::ForegroundRole); ui->tableView->model()->setData(index,"Pressed");
}
@