本文介绍了从pyqt中删除框架中的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些从布局中删除特定项目的示例,但是我仅从框架中删除所有内容就找不到任何东西.

There are some examples of removing specific items from a layout, but I can not find anything on simply deleting everything from a frame.

使用pyqt设计器,我创建了一个框架.然后使用pyuic4将文件转换为python.在主程序中,某些布局,项目和小部件会动态插入到框架中.但是,我实际上并不跟踪所有项目.刷新按钮后,我要删除框架中包含的所有内容,然后再次填充.

Using pyqt designer, I have created a frame. Then using pyuic4 the file is converted to python. In the main program, some layouts, items, and widgets are dynamically inserted to the frame. However, I dont actually keep track of all the items. On a button refresh, I want to delete everything contained in the frame and populate it again.

我的问题是,有没有一种简单的方法可以删除框架中包含的所有内容,包括布局,小部件和项目.

My question is, is there a simple way to delete everything that is contained in a frame, including layouts, widgets, and items.

到目前为止,我可以这样做:

As of now, I can do:

for i in range(len(MyResourceFrame.children())):
    MyResourceFrame.children()[i].deleteLater()

但是,我直接在其下有代码,在第一个qframe填充之后,单击repopulate会给出错误消息:已经有一个框架,然后删除了所有项目.在重新填充上单击第二个即可.这是否与后来的"想先超出范围有关?或者仅仅是一个名字?

However, I have code directly under that, and after the first qframe population, clicking on repopulate give the error that a frame is already there, which then removes all items. The second click on repopulate works. Does this have something to do with "Later" wanting to be out of scope first or is that just a name?

推荐答案

deleteLater 插槽只会安排要删除的对象.也就是说,在控制权返回事件循环之前,该对象将不会被删除(通常意味着当前执行的函数已返回之后).

The deleteLater slot will just schedule the object for deletion. That is, the object won't be deleted until control returns to the event loop (which will usually mean after the currently executing function has returned).

如果要立即删除对象,请使用 sip模块.这应该允许您删除布局及其所有包含的小部件,如下所示:

If you want to delete an object immediately, use the sip module. This should allow you delete a layout and all its contained widgets like this:

import sip
...

class Window(QtGui.QMainWindow):
    ...

    def populateFrame(self):
        self.deleteLayout(self.frame.layout())
        layout = QtGui.QVBoxLayout(self.frame)
        ...

    def deleteLayout(self, layout):
        if layout is not None:
            while layout.count():
                item = layout.takeAt(0)
                widget = item.widget()
                if widget is not None:
                    widget.deleteLater()
                else:
                    self.deleteLayout(item.layout())
            sip.delete(layout)

这篇关于从pyqt中删除框架中的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 21:49