Originally Posted by
Royceybaby
I have a subclassed QItemDelegate that does some formatting for my columns already. So adding a switch in to draw the child and parent rows differently is not a problem.
The only thing I can't get my head round is how will the QItemDelegate:
aint function know whether the item it is drawing is a parent item or child item and enter the correct switch path. I am sure it is simple but I just can't think how.
Royce
if you have only two levels then parent items have invalid parent (index.parent().isValid() gives false for top level items) and thier children have valid parents.
If your structure is more complicated then you can use Qt::UserRole and above to store your data:
With QStandardItemModel it would look like this:
// when creating an item you add:
item->setData(1, Qt::UserRole); // parent item
// or
item->setData(2, Qt::UserRole); // child
// then in delegate paint()
if (index.data(Qt::UserRole).toInt() == 1) {
// parent item
}
if (index.data(Qt::UserRole).toInt() == 2) {
// child item
}
// when creating an item you add:
item->setData(1, Qt::UserRole); // parent item
// or
item->setData(2, Qt::UserRole); // child
// then in delegate paint()
if (index.data(Qt::UserRole).toInt() == 1) {
// parent item
}
if (index.data(Qt::UserRole).toInt() == 2) {
// child item
}
To copy to clipboard, switch view to plain text mode
And if you have you own model class then return proper value (in my example it is 1 or 2) for Qt::UserRole in your model data() method.
Bookmarks