Answer the question
In order to leave comments, you need to log in
How to use QItemRole?
Good afternoon
Now I am adding functionality to the Model / View application, a question arose about the role for the model element. In my case, there is no need to use them, as it seems to me. If it is necessary to get data, then I request it from the model and then convert the QVariant to the data type I need. I would like to ask what is the advantage of roles, because they were invented for a reason.
Answer the question
In order to leave comments, you need to log in
The role tells the model how to display the data.
Usually, this is text, but data can also be displayed as tooltips, icons, background colors, and so on.
Here, for example, we set the background color:
QVariant ModelTable::data(const QModelIndex &index, int role) const {
//...
if (role == Qt::BackgroundRole)
return QBrush(QColor("#123"));
//...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question