开发者

PyQt4: Removing a child from its parent

I've created two uis using the Qt Designer, imported them into my script and set them in the normal way using the setupUi() method. When a button is clicked, and the appropriate method is executed, the new ui is loaded, but all of the widgets and connections from the old one persist.

What is the proper way to remove the connections and then delete all of the MainWindow's current children so that only the new ui's widgets and connections remain? Below is a simplified example of what I'm doing.

from PyQt4    import Q开发者_运维知识库tGui, QtCore
from Models   import *
from Backward import Ui_Backward
from Forward  import Ui_Forward

class MainWindow( QtGui.QMainWindow ):
  def __init__( self ):
    QtGui.QMainWindow.__init__( self, None )

    self.move_forward()

  def move_forward( self ):      
    self.ui = Ui_Forward()
    self.ui.setupUi( self )
    self.connect( self.ui.Button, QtCore.SIGNAL('clicked()'), self.move_backward )

  def move_backward( self ):      
    self.ui = Ui_Backward()
    self.ui.setupUi( self )
    self.connect( self.ui.Button, QtCore.SIGNAL('clicked()'), self.move_forward )

Once Ui_Forward is set and the button is pressed, Ui_Backward is set correctly, but all of the widgets from Ui_Forward are still in the MainWindow's list of children.


I'm trying to imagine what your goal is here. If you are trying to implement a wizard type application where the screen presents one set of controls and then switches to another screen with forward and backward buttons then you should try using a QStackedWidget.

A QStackedWidget will allow you to define one or more "pages" and switch between them. You design each page independently without (necessarily) having to move controls from one page to the next.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜