1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
.. currentmodule:: echo.qt
Interfacing with Qt widgets
---------------------------
When designing GUIs, it can be useful to have class properties link directly
to widgets. Echo currently includes functions to connect to Qt widgets, which
are described below. Contributions for other GUI frameworks are welcome!
We recommend using Qt libraries via the `QtPy
<https://pypi.python.org/pypi/QtPy/>`_ package, which provides a uniform API to
PyQt4, PyQt5, and PySide, and we use this in the following examples.
Let's assume we have a simple Qt window with a single checkbox::
from qtpy import QtWidgets
class MySimpleWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MySimpleWindow, self).__init__(parent=parent)
self.checkbox = QtWidgets.QCheckBox()
We can now add a callback property to the class and connect the property to the
Qt checkbox::
from qtpy import QtWidgets
from echo import CallbackProperty
from echo.qt import connect_checkable_button
class MySimpleWindow(QtWidgets.QMainWindow):
active = CallbackProperty(True)
def __init__(self, parent=None):
super(MySimpleWindow, self).__init__(parent=parent)
self.checkbox = QtWidgets.QCheckBox()
self.setCentralWidget(self.checkbox)
self._connection = connect_checkable_button(self, 'active', self.checkbox)
Note that the connection object needs to be stored in a variable, as one there
are no references left to the connection object, the connection is removed.
Let's now write a small script that uses this Qt window and adds a callback
when ``active`` is changed::
from echo import add_callback
def toggled(new):
print("Checkbox toggled: %s" % new)
app = QtWidgets.QApplication([''])
window = MySimpleWindow()
window.show()
add_callback(window, 'active', toggled)
app.exec_()
If you run this script, and click on the checkbox multiple times, the output
in the terminal will be::
Checkbox toggled: False
Checkbox toggled: True
Checkbox toggled: False
In addition, if you need to access the value of the checkbox in the code, you
can now simply access ``window.active`` rather than
``window.checkbox.isChecked``.
A number of other functions are available to connect to other types of widgets,
including combo boxes and text fields - see the API documentation for the
:ref:`qtapi` for more details.
|