autoconnect_callbacks_to_qt¶
- echo.qt.autoconnect_callbacks_to_qt(instance, widget, connect_kwargs={})[source]¶
Given a class instance with callback properties and a Qt widget/window, connect callback properties to Qt widgets automatically.
The matching is done based on the objectName of the Qt widgets. Qt widgets that need to be connected should be named using the syntax
type_namewheretypedescribes the kind of matching to be done, andnamematches the name of a callback property. By default, the types can be:value: the callback property is linked to a Qt widget that hasvalueandsetValuemethods. Note that for this type, two additional keyword arguments can be specified usingconnect_kwargs(see below): these arevalue_range, which is used for cases where the Qt widget is e.g. a slider which has a range of values, and you want to map this range of values onto a different range for the callback property, and the second islog, which can be set to True if this mapping should be done in log space.valuetext: the callback property is linked to a Qt widget that hastextandsetTextmethods, and the text is set to a string representation of the value. Note that for this type, an additional argumentfmtcan be provided, which gives either the format to use using the{}syntax, or should be a function that takes a value and returns a string. Optionally, if the Qt widget supports theeditingFinishedsignal, this signal is connected to the callback property too.bool: the callback property is linked to a Qt widget that hasisCheckedandsetCheckedmethods, such as a checkable button.text: the callback property is linked to a Qt widget that hastextandsetTextmethods. Optionally, if the Qt widget supports theeditingFinishedsignal, this signal is connected to the callback property too.combodata: the callback property is linked to a QComboBox based on theuserDataof the entries in the combo box.combotext: the callback property is linked to a QComboBox based on the label of the entries in the combo box.datetime: the callback property is linked to a QDateTimeEdit. Note that this connection will also work for the more specific QDateEdit and QTimeEdit widgets, as they are subclasses of QDateTimeEdit.
Applications can also define additional mappings between type and auto-linking. To do this, simply add a new entry to the
HANDLERSobject:>>> echo.qt.autoconnect import HANDLERS >>> HANDLERS['color'] = connect_color
The handler function (
connect_colorin the example above) should take the following arguments: the instance the callback property is attached to, the name of the callback property, the Qt widget, and optionally some keyword arguments.When calling
autoconnect_callbacks_to_qt, you can specifyconnect_kwargs, where each key should be a valid callback property name, and which gives any additional keyword arguments that can be taken by the connect functions, as described above. These include for examplevalue_range,log, andfmt.This function is especially useful when defining ui files, since widget objectNames can be easily set during the editing process.