Pyqt 小部件在被删除后仍然存在 [英] Pyqt widget lingers after being removed

查看:38
本文介绍了Pyqt 小部件在被删除后仍然存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QVBox 布局,其中包含一个 QVBox 布局和一个 QHBox 布局.我使用其他 QVBox 布局来保存动态创建的 GUI 对象,使用 QHBox 布局来保存添加/删除这些对象的按钮.如果我将 QHBox 放置在 QVBox 的顶部,一切正常,但是当我尝试将 QHBox 放置在 QVBox 下方时,对象不会被正确移除,而是在 QHBox 顶部徘徊".我会上传图片来演示问题.第一张是行动前,第二张是新建对象后,第三张是删除对象后

I have a QVBox layout that houses a QVBox layout and a QHBox layout. I use the other QVBox layout to hold dynamically created GUI objects and the QHBox layout to hold the buttons that add/remove those objects. Everything works correctly if I position the QHBox on top of the QVBox, but when I try to position the QHBox beneath the QVBox the objects aren't removed correctly but stay there "lingering" on top of the QHBox. I'll upload pictures to demonstrate the problem. First picture is before taking action, second is after creating a new object and third is after deleting the object

这是创建和删除新对象的代码

Here is the code that creates and deletes the new objects

    def addClient(self):
            if (len(self.clients) < 5):
                    client = clientComponent(self)
                    self.clients.append(client)
                    index = len(self.clients)-1
                    self.vLayout3.addWidget(self.clients[index])

                    client.setIndex(index)

                    self.clients[index].startButton.clicked.connect(partial(self.threadcontrol, '2', client.getIndex()))
                    self.clients[index].stopButton.clicked.connect(partial(self.clientstop, '0', client.getIndex()))

    def deleteClient(self):
            if (len(self.clients) > 1):
                    self.vLayout3.removeWidget(self.clients.pop())

这是我完成布局的地方

    def initializeUi(self):

            self.mainWidget = QWidget(self)
            self.setCentralWidget(self.mainWidget)

            self.mainLayout = QVBoxLayout(self.mainWidget)
            self.hLayout1 = QHBoxLayout()
            self.hLayout2 = QHBoxLayout()
            self.vLayout1 = QVBoxLayout()
            self.vLayout2 = QVBoxLayout()
            self.vLayout3 = QVBoxLayout()

            self.addServer()
            self.addClient()

            self.serverBox = QGroupBox('Server')
            self.clientBox = QGroupBox('Client')

            self.traffic1 = QLabel('0.0Mb/s', self)
            self.traffic1.setAlignment(Qt.AlignRight)
            self.traffic2 = QLabel('0.0Mb/s', self)
            self.traffic2.setAlignment(Qt.AlignCenter)
            self.traffic3 = QLabel('0.0Mb/s', self)
            self.traffic3.setAlignment(Qt.AlignLeft)

            self.newClientButton = QPushButton('+', self)
            self.deleteClientButton = QPushButton('-', self)

            self.hLayout1.addWidget(self.traffic1)
            self.hLayout1.addWidget(self.traffic2)
            self.hLayout1.addWidget(self.traffic3)
            self.hLayout2.addWidget(self.newClientButton)
            self.hLayout2.addWidget(self.deleteClientButton)
            self.vLayout2.addLayout(self.vLayout3)
            self.vLayout2.addLayout(self.hLayout2)

            self.mainLayout.addWidget(self.plot)
            self.mainLayout.addLayout(self.hLayout1)
            self.serverBox.setLayout(self.vLayout1)
            self.mainLayout.addWidget(self.serverBox)
            self.clientBox.setLayout(self.vLayout2)
            self.mainLayout.addWidget(self.clientBox)

推荐答案

发生这种情况是因为在从布局中删除客户端小部件后,主窗口仍然是客户端小部件的父窗口.如果您为小部件分配父小部件而不将其添加到任何布局,您将看到类似的行为.

This is happening because your main window remains the parent of the client widgets after you remove them from the layout. You will see similar behaviour if you assign a widget a parent widget without adding it to any layout.

移除父级应该可以解决问题.

Removing the parent should resolve the issue.

def deleteClient(self):
    if (len(self.clients) > 1):
        client = self.clients.pop()
        self.vLayout3.removeWidget(client)
        client.setParent(None)

您可能还需要调用 adjustSize调整窗口大小以适应剩余的小部件

You may also need to make a call to adjustSize to resize the window to fit the remaining widgets

这篇关于Pyqt 小部件在被删除后仍然存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆