How to debug PyQt applications without reloading?

I am using Qt + PyQt for development. My application is very, very large (20 MB of source code). It includes many different menus and widgets. Very often, I change only one line according to the method in some widgets. But to check the changes, I will turn off my application, restart it, go to menu1 β†’ menu2 .... and only after this step can I check the changes.

How can I reload methods for only one class / widget? Without a closed / open application?

+6
source share
1 answer

My general answer to this will not be, it is impossible in direct light. The main problem is that when you start the application, it loads everything into memory, and your classes are associated with their methods, etc. Therefore, to update the values ​​that you changed in the class method, you will have to destroy this class (or widget) and recreate it under the updated source code (which would also kill children).

Its bad looking, but it would look something like this:

## main.py ## class MainWindow(QtGui.QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.resize(640,480) self.central = widget.Widget() self.setCentralWidget(self.central) self.toolbar = QtGui.QToolBar() self.toolbar.addAction("Reload Entire Widget", self._reloadWidget) self.addToolBar(QtCore.Qt.TopToolBarArea, self.toolbar) def _reloadWidget(self): print "Reloading", widget reload(widget) self.central.deleteLater() self.central = widget.Widget() self.setCentralWidget(self.central) ## widget.py ## class Widget(QtGui.QWidget): def __init__(self, *args, **kwargs): super(Widget, self).__init__(*args, **kwargs) self.layout = QtGui.QVBoxLayout(self) self.button = QtGui.QPushButton("Click") self.layout.addWidget(self.button) self.button.clicked.connect(self.testMethod) def testMethod(self): print "testmethod(): FOO" 

Again, I think this approach is problematic and difficult to handle, since you need to destroy and recreate your widget. More problems arise when parts of your application have signals tied to the slots of the updated widget. Then you also need to reboot your modules and reconnect their signals. This becomes a big dependency tree situation. It is quite difficult to do with the standard python script, not to mention trying to do it with something more stable and dynamic, like the PyQt structure.

In principle, do not rely on this ability. You better set up something with QTest to simulate your repetitive tasks.

+3
source

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


All Articles