Reputation: 11
I am new to QT and have used QT 5 for programming in python. I have a main window but when I try to click a button for the second window, it shows but quickly disappears. Does anyone know how I can fix this issue?
def SecondWindow():
qw = QWidget()
qw.resize(800, 800)
qw.move(300, 300)
qw.show()
Upvotes: 1
Views: 282
Reputation: 98435
Python is different than C++: in the latter, the code would work as desired, even if you'd leak the widget. Technically we wouldn't leak the widget since it's accessible from QApplication::topLevelWidgets
.
In Python, there are no references to qw
after SecondWindow
finishes: it is a local variable. Thus the widget is destroyed immediately, since Python first uses reference counting for object lifetime management - and a garbage collector only to collect objects that form cycles, and that's not the case here.
The solution is to keep a reference to the widget you've created:
class MyClass(QObject):
@pyqtSlot()
def second_windowClick(self):
self.SecondWindow()
def SecondWindow(self):
qw = QWidget()
qw.resize(800, 800)
qw.move(300, 300)
qw.show()
self.qw = qw
Upvotes: 2