I have
QListView *myListView;
QStringList *myStringList;
QStringListModel *myListModel;
which I fill with data like this:
myStringList->append(QString::fromStdString(...));
myListModel->setStringList(*myStringList);
myListView->setModel(myListModel);
I want to change the font-color of some list entries, so I tried:
for (int i = 0; i < myListModel->rowCount(); ++i) {
std::cerr << myListModel->index(i).data().toString().toStdString() << std::endl;
myListModel->setData(myListModel->index(i), QBrush(Qt::green), Qt::ForegroundRole);
}
The data is print out to cerr correctly, but the color does not change. What am I missing?
QStringListModel
supports onlyQt::DisplayRole
andQt::EditRole
roles.You have to reimplement the
QStringListModel::data()
andQStringListModel::setData()
methods to support other roles.Example: