What is your model? Have you implemented the setData() method on it? If so, have you implemented it for the Qt:: BackgroundRole role?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?ui->HostList->model()
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Hi d_stranz,
Thanks for your reply,
I'm using a QStringListModel and my data gets populated as follows:
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?Qt Code:
{ int rv = OK; listView->setModel(listModel); delete oldModel; return rv; }To copy to clipboard, switch view to plain text mode
Thanks!
Last edited by cerr; 3rd October 2017 at 03:24.
And you implemented this -exact- code, right?I have copied the setRowColor() from the link above into my code base call it like
Qt Code:
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?
Qt Code:
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).
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Thank you d_stranz and everyone else involved ,
for future reference, I got an excellent working example here: https://forum.qt.io/post/418663
And I hope you realize that the critical line from that example:I got an excellent working example here
Qt Code:
// set the bg color of row 2To copy to clipboard, switch view to plain text mode
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?
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Good to know this. That's something that is not in the documentation and should be.Yes, I definitely do and that the problem is with QStringListModel not supporting the color setting, but QStandardItemModel in place works properly.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Bookmarks