Search code examples
pythonpython-3.xcomboboxpyqt5

getting past RuntimeError: wrapped C/C++ object of type ... has been deleted


When the loops are triggered for the first time, I store the combobox created in a list to retrieve them during the next trigger.

Going through my program after a few iteration, the runtime error is raised when trying to delete combobox from the grid layout


for self.combobox in self.itemlist_group:
    self.combobox.deleteLater()

for i in range(len(liste_X)):
    self.combobox = QtWidgets.QComboBox()
    self.grid_group.addWidget(self.combobox, i, 0)
    self.itemlist_group.append(self.combobox)    

I tried to understand why and used a try/except loop to find, and by iterating in the list of combobox, they were still there, but the error is still raised. The print() returned <class 'PyQt5.QtWidgets.QComboBox'>

I also tried using sip.isdeleted without success...

try:
    for self.combobox in self.itemlist_group:
        self.combobox.deleteLater()
    
    for i in range(len(liste_X)):
        self.combobox = QtWidgets.QComboBox()
        self.grid_group.addWidget(self.combobox, i, 0)
        self.itemlist_group.append(self.combobox)    
except RuntimeError:
    for self.combobox in self.itemlist_group:
        print(type(self.combobox))

Solution

  • Thanks to the solution of Musicamante, the solution is with the while loop:

    while self.itemlist_group: self.itemlist_group.pop().deleteLater()  
    

    instead of the first loop:

    for self.combobox in self.itemlist_group: self.combobox.deleteLater()