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. Unable to remove empty ListWidgetItem
Forum Updated to NodeBB v4.3 + New Features

Unable to remove empty ListWidgetItem

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 2 Posters 259 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.
  • S Offline
    S Offline
    sophia73583
    wrote on last edited by sophia73583
    #1

    Hi,
    I'm using pyside2, and trying to make a list of custom widgets with QListWidget.

    I created a custom widget with a delete button like this:

    class CustomWidget:
      deleted = Signal()
    
      def __init__()
        ...
        delete_button.clicked.connect(self.onDeleteClicked)
    
      def onDeleteClicked(self):
        self.deleted.emit()  
        self.deleteLater()
    

    And in the main widget with the ListWidget, I tried to connect the deleted signal to also remove the ListWidgetItem:

    class MainWidget:
      def addItem(self):
        ....
        list_item = QListWidgetItem(self.list_widget)
        item.list_item = list_item
        item.deleted.connect(self.removeItem(item))
    
      def removeItem(self, item):
        self.list_widget.removeItemWidget(item.list_item)  
        self.list_widget.takeItem(self.list_widget.row( item.list_item))
    

    I verified that removeItem was invoked when the custom widget was deleted, but the now empty ListWidgetItem stays there on the UI. And when I try to add things to the ListWidget again, a SegFault will be thrown.

    Any experts on how to correctly remove a ListWidgetItem including its widget?

    Update: Another thing I noticed was that even without clicking the delete button, removeItem() will always be called once in addItem() when the deleted signal is being connected. I assume this might have caused some reference to get lost at that stage, but why was it triggered when I'm just trying to connect the signal? (E.g. button.clicked.connect(onClicked) won't trigger onClicked unless actually clicked).

    JonBJ 1 Reply Last reply
    0
    • S sophia73583

      Hi,
      I'm using pyside2, and trying to make a list of custom widgets with QListWidget.

      I created a custom widget with a delete button like this:

      class CustomWidget:
        deleted = Signal()
      
        def __init__()
          ...
          delete_button.clicked.connect(self.onDeleteClicked)
      
        def onDeleteClicked(self):
          self.deleted.emit()  
          self.deleteLater()
      

      And in the main widget with the ListWidget, I tried to connect the deleted signal to also remove the ListWidgetItem:

      class MainWidget:
        def addItem(self):
          ....
          list_item = QListWidgetItem(self.list_widget)
          item.list_item = list_item
          item.deleted.connect(self.removeItem(item))
      
        def removeItem(self, item):
          self.list_widget.removeItemWidget(item.list_item)  
          self.list_widget.takeItem(self.list_widget.row( item.list_item))
      

      I verified that removeItem was invoked when the custom widget was deleted, but the now empty ListWidgetItem stays there on the UI. And when I try to add things to the ListWidget again, a SegFault will be thrown.

      Any experts on how to correctly remove a ListWidgetItem including its widget?

      Update: Another thing I noticed was that even without clicking the delete button, removeItem() will always be called once in addItem() when the deleted signal is being connected. I assume this might have caused some reference to get lost at that stage, but why was it triggered when I'm just trying to connect the signal? (E.g. button.clicked.connect(onClicked) won't trigger onClicked unless actually clicked).

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @sophia73583

       item.deleted.connect(self.removeItem(item))
      

      This is not the right way to connect a slot to pass it a parameter, and does not do what you think. You need:

       item.deleted.connect(lambda: self.removeItem(item))
      
      1 Reply Last reply
      1
      • S sophia73583 has marked this topic as solved on

      • Login

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