PDA

View Full Version : How to disable & enable a row in QTreeView



chithara
17th May 2019, 05:59
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

d_stranz
17th May 2019, 14:02
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.

chithara
20th May 2019, 05:42
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

d_stranz
21st May 2019, 00:27
I am not seeing any in-built setFlags() function on QModelIndex.

QAbstractItemModel::setFlags()

chithara
23rd May 2019, 10:55
Hi, I am not able to find any such setFlags() method under QAbstractItemModel :(:(

d_stranz
23rd May 2019, 16:33
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.

chithara
24th May 2019, 05:21
Thanks for your inputs. Will try