Important: Please read the Qt Code of Conduct -

Deleting all items in a tree view

  • How am I able to delete all items in a tree view? Using 'removeChild' function hasn't been working for me. Need to replace the windows macro 'TreeView_DeleteAllItems', which deletes all the items in the given tree view.

  • Moderators

    If you're working with a QTreeView then you probably have a custom model for it. Remove the data from the model between calls to beginResetModel and endResetModel. This will inform all attached views to query the model again and update their state.

    If you're working with the QTreeWidget it's as simple as calling clear.

  • @Chris-Kawa I am working with QTreeView. Can you provide some sort of example for using beginResetModel and endResetModel? I don't have much experience with them.

  • Moderators

    There's not much really to tell, but a (somewhat contrived) example would be this simple model of a vector of numbers:

    class MyModel : public QAbstractItemModel {
        QVector<int> data_;
        //the boring standard stuff
        MyModel(QObject* parent = nullptr) : QAbstractItemModel(parent) {}
        QModelIndex index(int row, int column, const QModelIndex &parent) const { return createIndex(row, column); }
        QModelIndex parent(const QModelIndex &child) const { return QModelIndex(); }
        int columnCount(const QModelIndex &parent) const { return 1; }
        int rowCount(const QModelIndex &parent) const { return parent.isValid() ? 0 : data_.size(); }
        QVariant data(const QModelIndex &index, int role) const {
            if(role == Qt::DisplayRole) return QString::number(;
            return QVariant();
        //the interesting stuff
        //adding a value
        void insertData(int position, int value) {
            beginInsertRows(QModelIndex(), position, position);
            data_.insert(position, value);
        //removing a value
        void removeData(int position) {
            beginRemoveRows(QModelIndex(), position, position);
        //clearing the whole thing:
        void clear() {

Log in to reply