Crash when using QListWidget::setItemWidget for the second time
-
@Bonnie "I thought the same way. So I changed
searchBartoQPointer<QLineEdit>.QPointerwill be set to nullptr when the object is deleted, but I checkedsearchBarafterremoveItemWidget(), and it wasn't nullptr. I also looked at the source code ofremoveItem(), and it actually just callssetItemWidget(item, nullptr), without performing any operations on itemWidget."@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
-
@Bonnie "I thought the same way. So I changed
searchBartoQPointer<QLineEdit>.QPointerwill be set to nullptr when the object is deleted, but I checkedsearchBarafterremoveItemWidget(), and it wasn't nullptr. I also looked at the source code ofremoveItem(), and it actually just callssetItemWidget(item, nullptr), without performing any operations on itemWidget." -
@Bonnie "I thought the same way. So I changed
searchBartoQPointer<QLineEdit>.QPointerwill be set to nullptr when the object is deleted, but I checkedsearchBarafterremoveItemWidget(), and it wasn't nullptr. I also looked at the source code ofremoveItem(), and it actually just callssetItemWidget(item, nullptr), without performing any operations on itemWidget."@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
-
@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
@Christian-Ehrlicher thanks
-
C chenly has marked this topic as solved on