I have copied the setRowColor() from the link above into my code base call it like
And you implemented this -exact- code, right?
{
if(!model)
return;
if(row<0 || row>=model->rowCount(parent))
return;
const int colCount = model->columnCount(parent);
for (int j = 0; j < colCount; ++j)
model->setData(model->index(row,j,parent),color,Qt::BackgroundRole);
}
void setRowColor(QAbstractItemModel* model, int row, const QBrush& color, const QModelIndex& parent = QModelIndex())
{
if(!model)
return;
if(row<0 || row>=model->rowCount(parent))
return;
const int colCount = model->columnCount(parent);
for (int j = 0; j < colCount; ++j)
model->setData(model->index(row,j,parent),color,Qt::BackgroundRole);
}
To copy to clipboard, switch view to plain text mode
"BackgroundRole" is one of the Qt::ItemDataRole enums.
Does this function work if you call it from somewhere beside the double-click slot? For example, in your ListViewPopulate method, what happens if you populate the list, then run a loop that sets every even-numbered entry to red background?
for ( int nRow = 0; nRow < listModel->rowCount(); nRow += 2 )
setRowColor( listModel, nRow, Qt::red );
for ( int nRow = 0; nRow < listModel->rowCount(); nRow += 2 )
setRowColor( listModel, nRow, Qt::red );
To copy to clipboard, switch view to plain text mode
By the way, your code in ListViewPopulate is a memory leak. Each time the function executes, it creates a new list model and sets it on the view, but you never delete the old list model (listView->model()). Instead for some strange reason, you delete the selection model (which is entirely different from the string list model you create and set on the view).
Bookmarks