[newbie] Set background of specific row in QListView
Hi,
I've found this: https://forum.qt.io/topic/76265/set-...-in-qtableview
I have copied the setRowColor() from the link above into my code base call it like:
Code:
void Widget
::on_HostList_doubleClicked(const QModelIndex &index
) {
setRowColor(model,index.row(),Qt::red,index);
}
but it does not seem to work, the color is not changing. Can someone help me here?
Re: [newbie] Set background of specific row in QListView
Quote:
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?
Re: [newbie] Set background of specific row in QListView
Hi d_stranz,
Thanks for your reply,
I'm using a QStringListModel and my data gets populated as follows:
Code:
{
int rv = OK;
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!
Re: [newbie] Set background of specific row in QListView
Quote:
I have copied the setRowColor() from the link above into my code base call it like
And you implemented this -exact- code, right?
Code:
{
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?
Code:
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).
Re: [newbie] Set background of specific row in QListView
Thank you d_stranz and everyone else involved ,
for future reference, I got an excellent working example here: https://forum.qt.io/post/418663
Re: [newbie] Set background of specific row in QListView
Quote:
I got an excellent working example here
And I hope you realize that the critical line from that example:
Code:
// 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?
Re: [newbie] Set background of specific row in QListView
Quote:
Originally Posted by
d_stranz
And I hope you realize that the critical line from that example:
Code:
// 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.
Re: [newbie] Set background of specific row in QListView
Quote:
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.