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. Do I need to call deleteLater() function if QTreeView has widget ownership?
Forum Update on Monday, May 27th 2025

Do I need to call deleteLater() function if QTreeView has widget ownership?

Scheduled Pinned Locked Moved Solved General and Desktop
qtreeviewqstandarditemmosetindexwidgetremoverowdeletelater
5 Posts 2 Posters 1.1k 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.
  • Y Offline
    Y Offline
    Yash001
    wrote on last edited by
    #1

    Hello,

    I am using QTreeView and QStandardItemModel to represent some data on Table. While representing the data I try to show some widget on table using function void QAbstractItemView::setIndexWidget(const QModelIndex &index, QWidget *widget). It is show correctly.

    Now main question is that if user try to set the new data on table at that time I am removing all previous data using bool QAbstractItemModel::removeRow(int row, const QModelIndex &parent = QModelIndex()).

    Do I require to call deleteLater() function while removing data? Or removing row from model is automatically delete the widget?

    auto wdg = treeWidget->indexWidget(index);
    wdg->disconnect();
    wdg->deleteLater();
    
    
    JonBJ 1 Reply Last reply
    0
    • Y Yash001

      Sorry For asking silly question. Here I got my ans RemoveRow From Model
      void QStandardItem::removeRow(int row)

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #5

      @Yash001
      When items are removed from a model the view will update to reflect the new state. Widgets you have added via setIndexWidget() which are no longer present will be deleted by Qt infrastructure without you needing to delete them or disconnect from signals.

      1 Reply Last reply
      2
      • Y Yash001

        Hello,

        I am using QTreeView and QStandardItemModel to represent some data on Table. While representing the data I try to show some widget on table using function void QAbstractItemView::setIndexWidget(const QModelIndex &index, QWidget *widget). It is show correctly.

        Now main question is that if user try to set the new data on table at that time I am removing all previous data using bool QAbstractItemModel::removeRow(int row, const QModelIndex &parent = QModelIndex()).

        Do I require to call deleteLater() function while removing data? Or removing row from model is automatically delete the widget?

        auto wdg = treeWidget->indexWidget(index);
        wdg->disconnect();
        wdg->deleteLater();
        
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @Yash001 said in Do I need to call deleteLater() function if QTreeView has widget ownership?:

        setIndexWidget

        https://doc.qt.io/qt-5/qabstractitemview.html#setIndexWidget says:

        Sets the given widget on the item at the given index, passing the ownership of the widget to the viewport.

        Y 2 Replies Last reply
        1
        • JonBJ JonB

          @Yash001 said in Do I need to call deleteLater() function if QTreeView has widget ownership?:

          setIndexWidget

          https://doc.qt.io/qt-5/qabstractitemview.html#setIndexWidget says:

          Sets the given widget on the item at the given index, passing the ownership of the widget to the viewport.

          Y Offline
          Y Offline
          Yash001
          wrote on last edited by
          #3

          @JonB Yes I know QTreeview has ownership of widget. If I will remove the row using model function then widget will delete automatically or not ?

          1 Reply Last reply
          0
          • JonBJ JonB

            @Yash001 said in Do I need to call deleteLater() function if QTreeView has widget ownership?:

            setIndexWidget

            https://doc.qt.io/qt-5/qabstractitemview.html#setIndexWidget says:

            Sets the given widget on the item at the given index, passing the ownership of the widget to the viewport.

            Y Offline
            Y Offline
            Yash001
            wrote on last edited by
            #4

            Sorry For asking silly question. Here I got my ans RemoveRow From Model
            void QStandardItem::removeRow(int row)

            JonBJ 1 Reply Last reply
            0
            • Y Yash001

              Sorry For asking silly question. Here I got my ans RemoveRow From Model
              void QStandardItem::removeRow(int row)

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #5

              @Yash001
              When items are removed from a model the view will update to reflect the new state. Widgets you have added via setIndexWidget() which are no longer present will be deleted by Qt infrastructure without you needing to delete them or disconnect from signals.

              1 Reply Last reply
              2

              • Login

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