本文介绍了PyQt4 - “运行时错误:底层 C/C 对象已被删除";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到这个 RuntimeError,我不知道如何解决.这就是我想要完成的.当我点击 QTreeView 中的不同项目时,我想用值动态更新这个 QTableWidget.在大多数情况下,我的代码可以正常工作,除非我点击我的第二​​个项目并且我需要更新我的 QTableWidgt,这是当我遇到这个运行时错误:底层 C/C 对象已被删除"时.这是我的代码片段:

I keep getting this RuntimeError which I'm not sure how to fix. Here's what I'm trying to accomplish. I want to update this QTableWidget with values dynamically as I'm clicking on different items in my QTreeView. On the most part, my code works except when I click on my second item and I need to update my QTableWidgt which is when I run into this "RuntimeError: underlying C/C object has been deleted". Here's a snippet of my code:

def BuildTable( self ):
    ...
    for label in listOfLabels :
        attr = self.refAttr[label]
        self.table.setItem(row, 0, QtGui.QTableWidgetItem( label ) )

        tableItem = QtGui.QTableWidgetItem( str(attr.GetValue()) )
        self.table.setItem(row, 1, tableItem )
        someFunc = functools.partial( self.UpdateValues, tableItem, label )

        QtCore.QObject.connect(self.table, QtCore.SIGNAL('itemChanged(QTableWidgetItem*)'), someFunc)

def UpdateValues(self, tableItem, label):
    print '--------------------------------'
    print 'UPDATING TEXT PROPERTY VALUE!!!'
    print tableItem.text()
    print label

编译器在一行中报错,print tableItem.text()"

The compiler complains errors on the line, "print tableItem.text()"

谢谢!

推荐答案

我相信问题在于您将回调与 QTableWidget 项绑定并建立了许多连接(坏).项目可以改变.因此,它们可能会被删除,从而使您的回调无效.

I believe the issue is that you are binding up a callback with a QTableWidget item and making many many connections (bad). Items can change. Thus, they can be deleted making your callback dead.

你想要的只是让 itemChanged 信号告诉你什么项目改变了,它发生的那一刻.

What you want is to just let the itemChanged signal tell you what item changed, the moment it happens.

self.table = QtGui.QTableWidget()
...
# only do this once...ever...on the init of the table object
QtCore.QObject.connect(
    self.table,
    QtCore.SIGNAL('itemChanged(QTableWidgetItem*)'),
    self.UpdateValues
)

然后在您的 SLOT 中,它会收到物品:

And then in your SLOT, it will receive the item:

def UpdateValues(self, tableItem):
    print '--------------------------------'
    print 'UPDATING TEXT PROPERTY VALUE!!!'
    print tableItem.text()

这篇关于PyQt4 - “运行时错误:底层 C/C 对象已被删除";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 04:00