Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QStandardItem::hasChildren returns true while removing last child



  • When I remove a row from my QTreeView, I got a currentRowChanged signal. In the slot, I check the hasChildren property of the parent item and I get true even if it was the last children.

    QTreeView * treeView;
    treeView = new QTreeView(PageHmiSetup);
    QStandardItemModel * deviceList;
    deviceList = new QStandardItemModel(this);
    ui->treeView->setModel(deviceList);
    
    connect (ui->treeView->selectionModel(),&QItemSelectionModel::currentRowChanged,this,&PageHmiSetup::showItemInfo);
    
    

    When I remove last child and parent get focus, showItemInfo() is called and ui->treeView->currentIndex()->hasChildren() returns true. I suspect that the current row is changed before actually removing the row: should I submit a bug description to Qt? Or is there anything else I did not understand?


  • Lifetime Qt Champion

    @Landolfi said in QStandardItem::hasChildren returns true while removing last child:

    I suspect that the current row is changed before actually removing the row: should I submit a bug description to Qt?

    Where do the docs state that currentRowChanged() must be emitted before the row is removed?



  • It's not a bug, the view updates the current index as a response to rowsAboutToBeRemoved so it's before the actual rows get removed so the behaviour you experience is totally normal and intended.

    To react after the rows have actually been removed add Qt::QueuedConnection as an extra argument to your connect()


Log in to reply