How to disable & enable a row in QTreeView
Hi, I have a QTreeView with custom model derived from QAbstractItemModel. I want to disable the parent item until all of its child items gets populated. Once done, then I have to enable the parent item. How can I achieve this using flags() function?
I want this kind of behavior for all the tree nodes to avoid user click till it is fully loaded.
Please help on this
Re: How to disable & enable a row in QTreeView
Code:
setFlags( index, flags( index ) ~ Qt::ItemIsEnabled ); // disables the item at index
setFlags( index, flags( index ) | Qt::ItemIsEnabled ); // enables the item at index
Should do it. Call the first one before you populate, call the second one after population is finished. You should probably call setExpanded( index, false ) on the tree view first to ensure that the parent is colapsed before populating the children.
Re: How to disable & enable a row in QTreeView
I am not seeing any in-built setFlags() function on QModelIndex.
auto flags = sourceIndex.flags();
flags.setFlag(Qt::ItemIsEnabled,false);
Don't know how to set this flag again to the sourceIndex
Re: How to disable & enable a row in QTreeView
Quote:
I am not seeing any in-built setFlags() function on QModelIndex.
QAbstractItemModel::setFlags()
Re: How to disable & enable a row in QTreeView
Hi, I am not able to find any such setFlags() method under QAbstractItemModel :(:(
Re: How to disable & enable a row in QTreeView
Sorry, sorry, my mistake. You need to implement the QAbstractItemModel::flags() method to return the correct set of flags for each item in your model. I was thinking of QTableWidgetItem, which does have a setFlags() method.
So your tree model should initially return flags without the Qt:: ItemIsEnabled bit set for parents whose children have not been populated. Once all of the children have been loaded, then your model can enable the parent index. I think emitting the dataChanged() signal will cause the view to request the new data and flags for the parent, or possibly just hovering the mouse over the parent item will cause the flags to be queried. You'll have to test that.
Re: How to disable & enable a row in QTreeView
Thanks for your inputs. Will try