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, but when I need to clear the QListWidget, I don't want the QWidget to be destructed because I need to reuse the QWidget."
@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
QListWidgetItemit will take your widget with it. To prevent that you must either remove it from theQListWidgetItemfirst (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.
-
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.
-
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 toQListWidgetis 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 aQListViewwhere you can swatch underlying model at will.