Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python removeWidget()是必需的吗_Python_User Interface_Pyqt_Pyqt4 - Fatal编程技术网

Python removeWidget()是必需的吗

Python removeWidget()是必需的吗,python,user-interface,pyqt,pyqt4,Python,User Interface,Pyqt,Pyqt4,我刚开始使用PyQt,我正在尝试对QGridLayout中的元素进行排序。在下面的代码中,我正在将一个现有按钮从单元格(4,1)移动到(0,0)。我的困惑是因为无论是否使用removeWidget,这两段代码的行为都完全相同: myButton = self.myGridLayout.itemAtPosition(4, 1).widget() self.myGridLayout.removeWidget(myButton) self.myGridLayout.addWid

我刚开始使用PyQt,我正在尝试对
QGridLayout
中的元素进行排序。在下面的代码中,我正在将一个现有按钮从单元格
(4,1)
移动到
(0,0)
。我的困惑是因为无论是否使用
removeWidget
,这两段代码的行为都完全相同:

    myButton = self.myGridLayout.itemAtPosition(4, 1).widget()
    self.myGridLayout.removeWidget(myButton)
    self.myGridLayout.addWidget(myButton, 0, 0)
同:

    myButton = self.myGridLayout.itemAtPosition(4, 1).widget()
    self.myGridLayout.addWidget(myButton, 0, 0)
PyQt是在幕后处理所有事情,还是有我没有注意到的事情发生


谢谢

一个小部件只能属于一个布局。在将小部件添加到布局之前,Qt将检查它是否曾经出现在布局中,如果是,它将从它当前所属的任何布局中删除它。该小部件还将自动重新租入布局的当前父小部件

一个小部件也不能被添加到同一个布局中两次,但这与上面的情况没有什么不同,因此它的处理方式完全相同