How QApplication() and QWidget() are connected?
This is an example code that I copied, it creates QApplication object and QWidget object, but there is no link between the two objects. I expected something like app.setWidget(did) to teach PySide/PyQt controller about the widget that was created.
# http://zetcode.com/gui/pysidetutorial/firstprograms/
# 1. PySide.QtGui is the class
import sys
from PySide import QtGui
# 2. setup the application
app = QtGui.QApplication(sys.argv)
# 3. create the widget and setup
wid = QtGui.QWidget()
wid.resize(250, 150)
wid.setWindowTitle('Simple')
# 4. Show the widget
wid.show()
# 5. execute the app
sys.exit(app.exec_())
What's the magic behind this?
QApplication is a singleton so it would be pretty easy, for QWidget to do: QApplication.instance() and interact with the QApplication instance.
In fact trying to instantiate QWidget before the QApplication leads to an error:
>>> QtGui.QWidget()
QWidget: Must construct a QApplication before a QPaintDevice
Which probably means this is what happens.
Edit: I've downloaded the qt sources and in fact, in src/gui/kernel/qwidget.cpp, line 328, there is:
if (!qApp) {
qFatal("QWidget: Must construct a QApplication before a QPaintDevice");
return;
}
Where qApp is a pointer to the QApplication instance(i.e. it is equivalent to calling QApplication.instance()).
So, in the end, the QWidget interacts with the QApplication via a global variable, even though it isn't necessary. They probably use qApp instead of QApplication.instance() to avoid unnecessary overhead that might happen when creating/destroying many QWidgets.
Related
I created UI using Qt Designer. Then I converted the ui file to a .py file (pyuic -x) - it works fine if launched directly. Then I tried to subclass my ui in a separate file to implement additional logic. And this is where things start to go wrong. Inheriting from QMainWindow and my Qt Designer file works OK with no issues, as expected. However, the moment I set any WindowFlag for my QMainWindow (any flag - I tried these: StaysOnTop, FramelessWindowHint) and run the file, the window appears and instantly disappears. The program continues to run in a console window, or as a PyCharm process, but the window is gone. It looks to me like it is getting out of scope - but why setting a simple flag would make any difference to the garbage collector? Could someone explain this behaviour?
Minimum code required to reproduce this phenomenon:
from ui import Ui_MainWindow
from PyQt5 import QtCore, QtWidgets, QtGui
import sys
class Logic(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setupUi(self)
self.show()
# self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
# self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_NoSystemBackground, True)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Logic()
sys.exit(app.exec_())
The window should appear and stay on the screen until one (or more) of the flags are uncommented. I use Python 3.8 (32-bit) with PyQt5. Run environment provided by PyCharm. Windows 10.
From the documentation of setWindowFlags():
Note: This function calls setParent() when changing the flags for a window, causing the widget to be hidden. You must call show() to make the widget visible again..
So, just move self.show() after setting the flags, or call it from outside the __init__ (after the instance is created), which is the most common and suggested way to do so, as it's considered good practice to show a widget only after it has been instanciated.
Don't critisize me using different classes - the reasons for his is becausethere will be more GUIs in my project created by the QtDesigner, but this should not be important for now.
In general, I have two Python scripts:
main.py:
from PyQt5 import QtCore, QtWidgets, QtGui
import sys
import time
from gui_class import Gui
app = QtWidgets.QApplication(sys.argv)
gui = Gui()
sys.exit(app.exec_())
gui_class.py:
from PyQt5 import QtWidgets
class Gui():
def __init__(self):
w = QtWidgets.QWidget()
w.resize(500, 500)
self.button = QtWidgets.QPushButton(w)
self.button.setGeometry(100, 100, 300, 300)
w.show()
If I run the main.py-script, then the window appears for a split second and disappears right away. I can't see it, I can't click it. The code does not terminate, though. It's still waiting for the application to finish - I can't do anything, though.
If I put a breakpoint before the line saying w.show() in the gui_class.py and simply continue the code after it stopped in that line, then the GUI is visible and I can click the button and the code terminates after I close the window - everything works as expected.
I am using PyQt5: 5.15.2 with Python3.7.
The problem is that w is a local variable that will be destroyed when the scope where it was created finishes executing. The solution is to extend its life cycle by increasing its scope by making it an attribute of the class, for this you must change w with self.w.
I developed two windows in QtDesigner (SourceForm, DestinationForm) and used pyuic5 to convert their .ui pages. I am using a third class WController as a way to navigate between the two windows using a stacked widget. I have a button in SourceForm that populates treeWidget with some data and the method handle_treewidget_itemchange dictates what happens when a particular item in treeWidget gets checked or unchecked by using self.treeWidget.itemChanged.connect(self.handle_treewidget_itemchange). It was my understanding that itemChanged.connect would automatically send the row and column of what was changed to the slot but when handle_treewidget_itemchange(self,row,col) gets called for the first time, my script crashes with a TypeError:
TypeError: handle_treewidget_itemchange() missing 2 required positional arguments: 'row' and 'col'
If I take out the row and col args, the script runs fine. When I originally had both the method and the call in the SourceForm .py file itself, my code worked as intended...maybe this is just a scope issue? I am beginning to think attempting to use PyQt while still inexperienced with Python a bad idea :(
I've tried to strip the code down to the essentials:
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import pyqtSlot
from imp_sourceform import Ui_SourceForm
from imp_destform import Ui_DestinationForm
class WController(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(WController, self).__init__(parent)
self.central_widget = QtWidgets.QStackedWidget()
self.setCentralWidget(self.central_widget)
self.sourcewindow = SourceForm()
self.destinationwindow = DestinationForm()
self.central_widget.addWidget(self.sourcewindow)
self.central_widget.addWidget(self.destinationwindow)
self.central_widget.setCurrentWidget(self.sourcewindow)
self.sourcewindow.selectdestinationsbutton.clicked.connect(lambda: self.navigation_control(1))
self.destinationwindow.backbutton.clicked.connect(lambda: self.navigation_control(0))
def navigation_control(self, topage):
if topage == 1:
self.central_widget.setCurrentWidget(self.destinationwindow)
elif topage == 0:
self.central_widget.setCurrentWidget(self.sourcewindow)
class SourceForm(QtWidgets.QWidget, Ui_SourceForm):
def __init__(self):
super(SourceForm, self).__init__()
self.setupUi(self)
self.treeWidget.itemChanged.connect(self.handle_treewidget_itemchange)
#pyqtSlot()
def handle_treewidget_itemchange(self,row,col):
if row.parent() is None and row.checkState(col) == QtCore.Qt.Unchecked:
for x in range(0,row.childCount()):
row.child(x).setCheckState(0, QtCore.Qt.Unchecked)
elif row.parent() is None and row.checkState(col) == QtCore.Qt.Checked:
for x in range(0,row.childCount()):
row.child(x).setCheckState(0, QtCore.Qt.Checked)
else:
pass
class DestinationForm(QtWidgets.QWidget, Ui_DestinationForm):
def __init__(self):
super(DestinationForm, self).__init__()
self.setupUi(self)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = WController()
window.show()
sys.exit(app.exec_())
You need to be careful when using pyqtSlot, as it is only too easy to clobber the signature of the slot it is decorating. In your case, it has re-defined the slot as having no arguments, which explains why you are getting that error message. The simple fix is to simply remove it, as your example will work perfectly well without it.
The main purpose of pyqtSlot is to allow several different overloads of a slot to be defined, each with a different signature. It may also be needed sometimes when making cross-thread connections. However, these use-cases are relatively rare, and in most PyQt/PySide applications it is not necessary to use pyqtSlot at all. Signals can be connected to any python callable object, whether it is decorated as a slot or not.
There's already an accepted answer to this question but I'll give mine anyway.
The problematic slot is connected to the itemChanged(QTreeWidgetItem *item, int column) signal, so the pyqtSlot should look like #pyqtSlot(QTreeWidgetItem, int).
Now, as ekhumoro pointed out, PyQt accepts to connect a signal to any Python callable, be it a method, a lambda, or an functor having a __call__ method. But it's less safe to do so rather than use #pyqtSlot.
For example, Qt automatically disconnects when either the source QObject (who would emit the signal) is destroyed, or the target QObject (who has the Qt slot) is destroyed. For example, if you remove a widget, it's not necessary to signal to it that something happened elsewhere. If you use #pyqtSlot, a real Qt slot is created in your class, so this disconnection mechanism can apply. Also, Qt doesn't hold a strong reference to the target QObject, so it can be deleted.
If you use any callable, for example a non-decorated, bound method, Qt will have no way to identify the target QObject of the connection. Worse, since you pass a Python callable, it will hold a strong reference to it, and the callable (the bound method) will in turn hold a reference to the final QObject, so your target QObject will not be garbage collected, until you manually disconnect it, or remove the source QObject.
See this code, you can enable one connection or the other, and observe the difference in behavior, which shows whether the window can be garbage-collected or not:
from PyQt5.QtWidgets import QApplication, QMainWindow
app = QApplication([])
def create_win():
win = QMainWindow()
win.show()
# case 1
# app.aboutToQuit.connect(win.repaint) # this is a qt slot, so win can be deleted
# case 2
# app.aboutToQuit.connect(win.size) # this is not a qt slot, so win can't be deleted
# win should get garbage-collected here
create_win()
app.exec_()
Can someone explain to me the difference between the following two code examples? Why is the top one not working? It executes without error, but the window doesn't stay open.
from PyQt4 import QtGui
import sys
app = QtGui.QApplication(sys.argv)
QtGui.QMainWindow().show()
app.exec_()
and:
from PyQt4 import QtGui
import sys
app = QtGui.QApplication(sys.argv)
win = QtGui.QMainWindow()
win.show()
app.exec_()
In QtGui.QMainWindow().show() you are creating an object of QMainWindow and you are showing it. But you do no save that instance of the QMainWindow in your memory. So eventually python's garbage collection deletes that instance and your QMainWindow no longer shows.
In the second code: win = QtGui.QMainWindow() you save the object instance of QMainWindow to win in your memory. Python does not consider that as garbage because it is in use and hence your window stays open
I have created a GUI with Qt Designer and accessed it via
def loadUiWidget(uifilename, parent=None):
loader = QtUiTools.QUiLoader()
uifile = QtCore.QFile(uifilename)
uifile.open(QtCore.QFile.ReadOnly)
ui = loader.load(uifile, parent)
uifile.close()
return ui
MainWindow = loadUiWidget("form.ui")
MainWindow.show()
children = MainWindow.children()
button1 = MainWindow.QPushButton1
"children" does already contain the widgets "QPushButton1", "QTextBrowser1" created in the UI but shouldn't the be accessed by the recoursive findChildren() method?
What is an elegant way to access the widgets of the .ui File?
References:
Find correct instance,
Load .ui file
Since widget names in Qt Designer must be unique, the hierarchy (at least for getting references to the widgets) is flattened (with no risk of conflict), and so the best way is just to access them via:
loader = QtUiTools.QUiLoader()
ui = loader.load('filename.ui', parent)
my_widget = ui.my_widget_name
This would place a reference to the widget called 'my_widget_name' in Qt Designer in the variable my_widget.
I would say the above is the most pythonic way of accessing the widgets created when you load the .ui file.
There are two disadvantages of loading UI at run time:
overhead each time the program is run (actually, each time the loader is used)
lack of support of code completion and checking, since IDE doesn't know the code behind ui until the uifile has been loaded.
An alternative, assuming you are using the modern version of PySide called "Qt for Python", is to "compile" the .ui file to a Python class (see docs). For this, after saving filename.ui, execute
pyside2-uic filename.ui -o ui_mainwindow.py
while within your virtual environment, if any. The new class will be called Ui_MainWindow. Assuming you have a text_box widget in your UI, you can now access its properties and methods. Here is a full working example:
import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from ui_mainwindow import Ui_MainWindow
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.text_box.setPlainText('test') # here we are addressing widget
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Notes:
pyside2-uic should be called after every change of the .ui file. This is a disadvantage of this approach in comparison to OP. It also means that you should either version-control both .ui and .py files for your UI, or somehow call uic during deployment.
The big advantage is that IDE like PyCharm has access to all widget methods and properties for autocompletion and code checking.
As of today, pyside2-uic creates non-PEP8 compliant code. However, as long as you give your widgets PEP8-compliant names, your own code will be OK.