Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QListWidget: is there a way to clear the list but cache the custom widget on item(Updated)?
Forum Updated to NodeBB v4.3 + New Features

QListWidget: is there a way to clear the list but cache the custom widget on item(Updated)?

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 809 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IvanIsLearning
    wrote on last edited by IvanIsLearning
    #1

    Hello,
    I am reusing a QListWidget to display different content. The content is displayed in a custom widget by putting the custom widget inside the list by QListWidget.setItemWidget(item, widget).

    I use QtWidgets.QListWidget.takeItem(row) to save the QListWidgetItem, but when I add the item to list again, my custom widget is no where to be found(seems only the item is added and my custom widget on the item is lost).

    It seems that my custom widget added by setItemWidget() is destroyed when I takeItem() the corresponding item.

    Here is my Code:

    import sys
    from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QListWidget, QListWidgetItem, QVBoxLayout, QLabel
    
    class MyList(QWidget):
      def __init__(self):
        super().__init__()
        self.cache = None
        self.layout = QVBoxLayout(self)
    
        self.listWidget = QListWidget()
        self.clearButton = QPushButton('Cache And Clear', clicked = self.cacheAndClear)
        self.loadCacheButton = QPushButton('Load Cache', clicked = self.loadCache)
        self.layout.addWidget(self.listWidget)
        self.layout.addWidget(self.clearButton)
        self.layout.addWidget(self.loadCacheButton)
        
        self.customWidget = QLabel('Assume this to be my custom widget.')
        item = QListWidgetItem()
        self.listWidget.addItem(item)
        self.listWidget.setItemWidget(item, self.customWidget)
      
      def cacheAndClear(self):
        self.myWidget = self.listWidget.itemWidget(self.listWidget.item(0))
        self.cache = self.listWidget.takeItem(0)
      
      def loadCache(self):
        if not self.cache: return
        newItem = QListWidgetItem()
        self.listWidget.addItem(newItem)
        self.listWidget.setItemWidget(newItem, self.myWidget)
    
    if __name__ == '__main__':
      app = QApplication()
      mylist = MyList()
      mylist.show()
      sys.exit(app.exec())
    

    And the error log when I click "Load Cache" button:

    RuntimeError: Internal C++ object (PySide6.QtWidgets.QLabel) already deleted.
    

    So, my question:
    Is there a way to cache the custom widget(the widget on the item, not the item itself) after clearing a list?
    (I tried clear() the list, takeItem(), and replace my custom widget with other widget before clear(). And my custom widget got destroyed anyway no matter what I do.)

    Update:
    Ok, I find a way around: using multiple QListWidget on a QStackedWidget. So instead of clearing and resuing one list, I just leave my custom widget on the list and switch between multiple lists.
    I will leave this unsolved, in case there is actually a way to cache the custom widget.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Aliviya
      wrote on last edited by
      #2

      No, there is no way to cache the custom widget after clearing a list. Since the custom widget is linked to the QListWidgetItem, once you take that item away, the custom widget is destroyed. If you want to reuse the custom widget, you will have to keep a reference to it and add it back in when you need it.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved