How to create a new PySide / PyQt window button?

I am having problems with the "New Window" feature in PyQt4 / PySide with Python 2.7. I hooked up the initNewWindow() function to create a new window, execute the action, and put it on the menu bar. Once a common feature in desktop software. Instead of giving me a new permanent window next to another, a new window opens and closes. The code I'm working on is proprietary, so I created an example that does the same with the same error below. Is there any way to make this work? Works in PySide with Python 2.7. It has been written and tested on Windows.

 from PySide.QtCore import QSize from PySide.QtGui import QAction from PySide.QtGui import QApplication from PySide.QtGui import QLabel from PySide.QtGui import QMainWindow from PySide.QtGui import QMenuBar from PySide.QtGui import QMenu from sys import argv def main(): application = QApplication(argv) window = QMainWindow() window.setWindowTitle('New Window Test') menu = QMenuBar(window) view = QMenu('View') new_window = QAction('New Window', view) new_window.triggered.connect(initNewWindow) view.addAction(new_window) menu.addMenu(view) label = QLabel() label.setMinimumSize(QSize(300,300)) window.setMenuBar(menu) window.setCentralWidget(label) window.show() application.exec_() def initNewWindow(): window = QMainWindow() window.setWindowTitle('New Window') window.show() if __name__ == '__main__': main() 
+4
source share
2 answers

When initNewWindow() returns, the window variable is deleted, and the window reference count drops to zero, causing the new C ++ object to be deleted. That is why your window closes immediately.

If you want to keep it open, be sure to keep the link. The easiest way to do this is to make your new window a child of the calling window and set its WA_DeleteOnClose widget WA_DeleteOnClose (see Qt::WidgetAttribute ).

+3
source

If the function creates a PyQt object that the application should continue to use, you will need to make sure that the link to it is stored somehow. Otherwise, it can be removed by the Python garbage collector immediately after the function returns.

Thus, either give the object a parent object, or save it as an attribute of some other object. (In principle, an object could also be made a global variable, but this is usually considered bad practice).

Here is a revised version of your sample script that demonstrates how to fix your problem:

 from PySide import QtGui, QtCore class Window(QtGui.QMainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) menu = self.menuBar().addMenu(self.tr('View')) action = menu.addAction(self.tr('New Window')) action.triggered.connect(self.handleNewWindow) def handleNewWindow(self): window = QtGui.QMainWindow(self) window.setAttribute(QtCore.Qt.WA_DeleteOnClose) window.setWindowTitle(self.tr('New Window')) window.show() # or, alternatively # self.window = QtGui.QMainWindow() # self.window.setWindowTitle(self.tr('New Window')) # self.window.show() if __name__ == '__main__': import sys app = QtGui.QApplication(sys.argv) window = Window() window.resize(300, 300) window.show() sys.exit(app.exec_()) 
+5
source

Source: https://habr.com/ru/post/1385943/


All Articles