无法更新UI因为无法删除小部件

时间:2012-03-07 12:11:16

标签: pyqt4 pyside

我正在创建一个具有动态用户界面的应用程序,这意味着小部件应该出现并根据数据库中的数据消失。代码如下所示。

数据部分正常,UI的更新没有。

我在QTabWidget中动态创建一个小部件。这个小部件(作为一个类)包含3个小部件:2个listWidgets和一个按钮。删除或调用destroy-method,无论是否有widget.update或widget.repaint都不成功。

我做错了什么?

class ImEntityWidget(object):
    def __init__(self, widgetParent, entityclass, x, y, width, height):
        self.widgetParent = widgetParent
        self.entityclass = entityclass

        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.focusItem = session.query(Entity).filter(
                         Entity.parent_vpu_id == VpuFocus.Id).filter(
                         Entity.Class == self.entityclass).first()

        self.listWidgetHigher = QListWidget(self.widgetParent)
        self.listWidgetHigher.setGeometry(QtCore.QRect(
                                          self.x, self.y, self.width, 25))
        font = QtGui.QFont()
        font.setPointSize(8)
        self.listWidgetHigher.setFont(font)
        self.listWidgetHigher.setObjectName("listWidgetHigher" +
                                            str(self.entityclass.name))

        # Entity in focus pushbutton widget
        self.listWidget_pb = QtGui.QPushButton(self.widgetParent)
        self.listWidget_pb.setGeometry(QtCore.QRect(
                                       self.x, self.y + 25, self.width, 25))
        font = QtGui.QFont()
        font.setPointSize(7)
        font.setWeight(75)
        font.setBold(True)
        self.listWidget_pb.setFont(font)
        self.listWidget_pb.setObjectName("pushButton" + str(self.entityclass.name))

        # Lower recursion entity widget
        self.listWidget = QListWidget(self.widgetParent)
        self.listWidget.setGeometry(QtCore.QRect(
                        self.x, self.y + 50, self.width, self.height - 50))
        font = QtGui.QFont()
        font.setPointSize(8)
        self.listWidget.setFont(font)
        self.listWidget.setObjectName("listWidget" + str(self.entityclass.name))

        # set button context menu policy
        self.listWidget_pb.setContextMenuPolicy(Qt.CustomContextMenu)
        self.listWidget_pb.customContextMenuRequested.connect(
                                                    self.listWidget_pbCtxMenu)
        self.listWidget_pb.connect(self.listWidget_pb,
                SIGNAL("activated(QModelIndex)"), self.focusHigherRecursion)
        self.listWidget.itemDoubleClicked.connect(self.doubleClick)


#Using the widget class
self.entWidget = ImEntityWidget(
              self.tabPrimaryProcesses, entityclass,
              wX, wY, wWidth, wHeight)
self.entityWidgets.append(self.entWidget)



# Trying to delete the widgets
for self.widget in self.entityWidgets:
    #del self.widget.listWidgetHigher
    #del self.widget.listWidget_pb
    #del self.widget.listWidget
    #self.tabPrimaryProcesses.update()
    self.widget.listWidgetHigher.destroy(destroyWindow=True)
    self.widget.listWidget_pb.destroy(destroyWindow=True)
    self.widget.listWidget.destroy(destroyWindow=True)

1 个答案:

答案 0 :(得分:0)

杰夫是对的,要删除你应该尝试的小部件:

self.widget.listWidgetHigher.setParent(None)
self.widget.listWidget_pb.setParent(None)
self.widget.listWidget.setParent(None)