PDA

View Full Version : [newbie] Set background of specific row in QListView



cerr
2nd October 2017, 02:24
Hi,

I've found this: https://forum.qt.io/topic/76265/set-background-of-specific-row-in-qtableview
I have copied the setRowColor() from the link above into my code base call it like:


void Widget::on_HostList_doubleClicked(const QModelIndex &index)
{
QAbstractItemModel *model = ui->HostList->model();
setRowColor(model,index.row(),Qt::red,index);
}
but it does not seem to work, the color is not changing. Can someone help me here?

d_stranz
2nd October 2017, 17:53
ui->HostList->model()

What is your model? Have you implemented the setData() method on it? If so, have you implemented it for the Qt:: BackgroundRole role?

cerr
3rd October 2017, 03:20
Hi d_stranz,

Thanks for your reply,
I'm using a QStringListModel and my data gets populated as follows:

int JTFTools::ListViewPopulate(QListView *listView, QStringList *stringList)
{
int rv = OK;

QStringListModel* listModel = new QStringListModel(*stringList, listView);
QItemSelectionModel *oldModel = listView->selectionModel();
listView->setModel(listModel);

delete oldModel;

return rv;
}
setData() is here: http://doc.qt.io/qt-5/qstringlistmodel.html#setData but I couldn't find Qt:: BackgroundRole, how do I go about this?
Thanks!

d_stranz
3rd October 2017, 18:35
I have copied the setRowColor() from the link above into my code base call it like

And you implemented this -exact- code, right?



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);
}

"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 );


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

cerr
4th October 2017, 05:33
Thank you d_stranz and everyone else involved ,
for future reference, I got an excellent working example here: https://forum.qt.io/post/418663

d_stranz
4th October 2017, 16:14
I got an excellent working example here

And I hope you realize that the critical line from that example:



// set the bg color of row 2
model->setData(model->index(1, 0), QBrush(Qt::red), Qt::BackgroundRole);


is no different from what you posted in the setRowColor() code and is no different from what I suggested to you to try in my previous post, right?

cerr
5th October 2017, 06:13
And I hope you realize that the critical line from that example:



// set the bg color of row 2
model->setData(model->index(1, 0), QBrush(Qt::red), Qt::BackgroundRole);


is no different from what you posted in the setRowColor() code and is no different from what I suggested to you to try in my previous post, right?
Yes, I definitely do and that the problem is with QStringListModel not supporting the color setting, but QStandardItemModel in place works properly.

d_stranz
5th October 2017, 20:38
Yes, I definitely do and that the problem is with QStringListModel not supporting the color setting, but QStandardItemModel in place works properly.

Good to know this. That's something that is not in the documentation and should be.