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. Crash when using QListWidget::setItemWidget for the second time
Forum Updated to NodeBB v4.3 + New Features

Crash when using QListWidget::setItemWidget for the second time

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 379 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.
  • C Offline
    C Offline
    chenly
    wrote on last edited by chenly
    #1
    This post is deleted!
    1 Reply Last reply
    0
    • C chenly

      @Bonnie "I thought the same way. So I changed searchBar to QPointer<QLineEdit>. QPointer will be set to nullptr when the object is deleted, but I checked searchBar after removeItemWidget(), and it wasn't nullptr. I also looked at the source code of removeItem(), and it actually just calls setItemWidget(item, nullptr), without performing any operations on itemWidget."

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #4

      @chenly Then you did not look correctly in to Qt source code:

      void QAbstractItemView::setIndexWidget(const QModelIndex &index, QWidget *widget)
      {
          Q_D(QAbstractItemView);
          if (!d->isIndexValid(index))
              return;
          if (indexWidget(index) == widget)
              return;
          if (QWidget *oldWidget = indexWidget(index)) {
              d->persistent.remove(oldWidget);
              d->removeEditor(oldWidget);
              oldWidget->removeEventFilter(this);
              oldWidget->deleteLater(); <--- here
          }
      

      See also https://doc.qt.io/qt-6/qabstractitemview.html#setIndexWidget

      /edit: the QListWidget documentation was missing a note about the ownership: https://codereview.qt-project.org/c/qt/qtbase/+/588117

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      C 1 Reply Last reply
      2
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by
        #2

        Because removeItemWidget will delete the widget.

        C 1 Reply Last reply
        3
        • B Bonnie

          Because removeItemWidget will delete the widget.

          C Offline
          C Offline
          chenly
          wrote on last edited by chenly
          #3

          @Bonnie "I thought the same way. So I changed searchBar to QPointer<QLineEdit>. QPointer will be set to nullptr when the object is deleted, but I checked searchBar after removeItemWidget(), and it wasn't nullptr. I also looked at the source code of removeItem(), and it actually just calls setItemWidget(item, nullptr), without performing any operations on itemWidget."

          Christian EhrlicherC 1 Reply Last reply
          0
          • C chenly

            @Bonnie "I thought the same way. So I changed searchBar to QPointer<QLineEdit>. QPointer will be set to nullptr when the object is deleted, but I checked searchBar after removeItemWidget(), and it wasn't nullptr. I also looked at the source code of removeItem(), and it actually just calls setItemWidget(item, nullptr), without performing any operations on itemWidget."

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #4

            @chenly Then you did not look correctly in to Qt source code:

            void QAbstractItemView::setIndexWidget(const QModelIndex &index, QWidget *widget)
            {
                Q_D(QAbstractItemView);
                if (!d->isIndexValid(index))
                    return;
                if (indexWidget(index) == widget)
                    return;
                if (QWidget *oldWidget = indexWidget(index)) {
                    d->persistent.remove(oldWidget);
                    d->removeEditor(oldWidget);
                    oldWidget->removeEventFilter(this);
                    oldWidget->deleteLater(); <--- here
                }
            

            See also https://doc.qt.io/qt-6/qabstractitemview.html#setIndexWidget

            /edit: the QListWidget documentation was missing a note about the ownership: https://codereview.qt-project.org/c/qt/qtbase/+/588117

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            C 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              @chenly Then you did not look correctly in to Qt source code:

              void QAbstractItemView::setIndexWidget(const QModelIndex &index, QWidget *widget)
              {
                  Q_D(QAbstractItemView);
                  if (!d->isIndexValid(index))
                      return;
                  if (indexWidget(index) == widget)
                      return;
                  if (QWidget *oldWidget = indexWidget(index)) {
                      d->persistent.remove(oldWidget);
                      d->removeEditor(oldWidget);
                      oldWidget->removeEventFilter(this);
                      oldWidget->deleteLater(); <--- here
                  }
              

              See also https://doc.qt.io/qt-6/qabstractitemview.html#setIndexWidget

              /edit: the QListWidget documentation was missing a note about the ownership: https://codereview.qt-project.org/c/qt/qtbase/+/588117

              C Offline
              C Offline
              chenly
              wrote on last edited by
              #5

              @Christian-Ehrlicher thanks

              1 Reply Last reply
              0
              • C chenly has marked this topic as solved on

              • Login

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