When a QListWidgetItem is removed, how can I prevent the attached QWidget from being destructed?
-
@kuqipair said in When a QListWidgetItem is removed, how can I prevent the attached QWidget from being destructed?:
I want to attach a custom QWidget to a QListWidget
And what call do you make for that? void QListWidget::setItemWidget(QListWidgetItem *item, QWidget *widget)?
Note: The list takes ownership of the widget.
So assuming that if you delete the
QListWidgetItem
it will take your widget with it. To prevent that you must either remove it from theQListWidgetItem
first (presumablysetItemWidget(nullptr)
?) or you could useQListWidget::takeItem()
which hands ownership of the wholeQListWidgetItem
(with its content) over to you. -
Actually, what I want to achieve is the ability to switch between multiple different lists within the same QListWidget. Each QListWidgetItem is a custom QWidget, and I don't want to call delete every time I switch to a different view. I found that no matter what I do, the QWidget always gets destructed.
-
@kuqipair
As @Pl45m4 says adding widgets toQListWidget
is costly and to be avoided for preference.the ability to switch between multiple different lists within the same QListWidget
This sounds like a reason not to use a
QListWidget
, which has its own internal data model. Rather aQListView
where you can swatch underlying model at will.