Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Ensure garbage collection on QListWidget items.



  • Does QListWidget::clear() delete all dynamically allocated elements it contains in Qt4.8? I'm not sure how to interpret the documentation, and I want to ensure I'm doing this properly.

    I populate my list with custom QWidget HistoryItem as shown:

        HistoryItem *historyItem = new HistoryItem(this, ...some data parameters...);
        QListWidgetItem *qlwItem = new QListWidgetItem();
        qlwItem->setSizeHint(QSize(0, 70));
        ui->listWidget->addItem(qlwItem);
        ui->listWidget->setItemWidget(qlwItem, historyItem);
    

    I'm not worried about the HistoryItems as they get a pointer to the parent QWidget. But the QListWidgetItems don't have that. If they're not deleted with QListWidget::clear(), can I use QListWidget::itemFromIndex() to get the pointers for deletion? Is that the correct procedure?

    Thank you for the help.

    And I know 4.8 is OLD. (Sorry!) Porting to 5 will happen in the future, but it's not feasible at the moment for toolchain and time reasons.


  • Lifetime Qt Champion

    Hi,

    As the warning state, all items are deleted.


Log in to reply