QTreeView doesn't change its behaviour for child items even if flags are correctly set
Solved
General and Desktop
-
Hello,
I have a very simple Model/View with a custom model and a default QTreeView.My model has 2 levels only:
I have 6 columns:
- Type, Name, and Life belong to the top level item
- Twin, Work, and Usage belong to the childrens
I need to:
- Edit 'Name' and 'Life'
- Edit 'Work'
- Make anything selectable
- Make children be draggable as they will be copied on another widget
I've set the flags like this:
constexpr int TYPE_COL = 0; constexpr int NAME_COL = 1; constexpr int LIFE_COL = 2; constexpr int TWIN_SUBINDEX_COL = 3; constexpr int TWIN_WORK_COL = 4; constexpr int TWIN_USAGE_COL = 5; Qt::ItemFlags MyModel::flags(const QModelIndex &index) const { if(!index.isValid()) return Qt::NoItemFlags; int col = index.column(); if(getType(index.internalPointer()) == AbstractToolItem::ToolType::TOP_LEVEL) { switch(col) { case TYPE_COL: return Qt::ItemIsSelectable | Qt::ItemIsEnabled; case NAME_COL: case LIFE_COL: return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable; } } else { switch(col) { case TWIN_SUBINDEX_COL: case TWIN_USAGE_COL: return Qt::ItemIsSelectable | Qt::ItemIsEnabled; case TWIN_WORK_COL: return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemNeverHasChildren | Qt::ItemIsDragEnabled; } } return Qt::NoItemFlags; }
Just in case,
MyModel::data(....)
returns aQVariant()
for "invalid columns":- Column 3, 4, 5 for top level items
- Column 0, 1, 2 for childrens
Everything seems to work only for the top level items and I don't know why...
Any ideas? -
A colleague of mine, that used Qt for years, helped me.
The culprit was (other than me) that I wrongly created the parent of the children.