Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-39324

Crash when inserting twice the same widget in QAbstractItemView

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: P2: Important P2: Important
    • None
    • 5.2.1, 5.3.0
    • Widgets: Itemviews
    • None
    • All.

      Given an overly-simplified code like this:

      QAbstractItemView* some_view = ...;
      QWidget* some_widget = ...;
      QModelIndex some_index = ...;
      some_view->setIndexWidget(some_index, some_widget); // (1)
      some_view->setIndexWidget(some_index, some_widget); // (2)
      

      Then this code will lead to a crash, because the widget inserted in (1) will be unconditionnally deleted when calling (2)... despite the fact it's actually the same widget.

      The following fix is suggested, to be inserted in QAbstractItemView::setIndexWidget(), line 3124 :

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

      Basically, just do nothing if inserting again the same widget.

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            richard Richard Moe Gustavsen
            ybailly@vero Yves Bailly
            Votes:
            2 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes