Understanding the “underlying C/C++ object has been deleted” error

前端 未结 5 1772
故里飘歌
故里飘歌 2020-12-24 14:00

It\'s not the first time I am getting the RuntimeError: underlying C/C++ object has been deleted. I\'ve solved it many times altering my code in a random but in

5条回答
  •  天涯浪人
    2020-12-24 14:36

    @charley is completely right, he explained the theory very well. Although in practice this ownership problem can happen in many scenarios, one of the most common is forgetting to call the constructor of the base class while subclassing a QT class - from time to time I always get stuck with this when I start coding from scratch.

    Take this very common example of subclassing a QAbstractTableModel:

    from PyQt4.QtCore import *
    
    
    class SomeTableModel(QAbstractTableModel):
    
      def __init__(self, filename):
        super(SomeTableModel, self).__init__() # If you forget this, you'll get the
                                               # "underlying C/C++ object has been 
                                               # deleted" error when you instantiate
                                               # SomeTableModel.
    

提交回复
热议问题