Question

I have two custom widgets (two classes based on QtGui.QWidget). In __init__ of QtGui.QMainWindow I create their instances:

self.MyWidget1 = MyWidget1()
self.MyWidget2 = MyWidget2()

There are also two buttons (QtGui.QPushButton) in __init__ part, and there are two slots when user clicks each of them:

def clickButton1(self):
  self.setCentralWidget(self.MyWidget1)

def clickButton2(self):
  self.setCentralWidget(self.MyWidget2)

But it works only on first click and then PyQt says that underlying widget (MyWidget1 or MyWidget2) was deleted. I think it was done by sip module. Is there a way to prevent deleting widgets after reseting of central widget? Thanks!

Was it helpful?

Solution

I think it's almost impossible.

From setCentralWidget docs:

Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.

So, you should create new MyWidget instance.

def clickButton2(self):
    self.setCentralWidget(MyWidget2())

But the right way of doing such things is to use QStackedWidget

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top