Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Deleting all items in a tree view
Forum Update on Monday, May 27th 2025

Deleting all items in a tree view

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 6.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    joeydonovan4
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      J 1 Reply Last reply
      0
      • Chris KawaC Chris Kawa

        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.

        J Offline
        J Offline
        joeydonovan4
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 {
          private:
              QVector<int> data_;
          public:
              //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(data_.at(index.row()));
                  return QVariant();
              }
          
              //the interesting stuff
              //adding a value
              void insertData(int position, int value) {
                  beginInsertRows(QModelIndex(), position, position);
                  data_.insert(position, value);
                  endInsertRows();
              }
              //removing a value
              void removeData(int position) {
                  beginRemoveRows(QModelIndex(), position, position);
                  data_.remove(position);
                  endRemoveRows();
              }
              //clearing the whole thing:
              void clear() {
                  beginResetModel();
                  data_.clear();
                  endResetModel();
              }
          };
          
          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved