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_name
wheretype
describes the kind of matching to be done, andname
matches the name of a callback property. By default, the types can be:value
: the callback property is linked to a Qt widget that hasvalue
andsetValue
methods. 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 hastext
andsetText
methods, and the text is set to a string representation of the value. Note that for this type, an additional argumentfmt
can 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 theeditingFinished
signal, this signal is connected to the callback property too.bool
: the callback property is linked to a Qt widget that hasisChecked
andsetChecked
methods, such as a checkable button.text
: the callback property is linked to a Qt widget that hastext
andsetText
methods. Optionally, if the Qt widget supports theeditingFinished
signal, this signal is connected to the callback property too.combodata
: the callback property is linked to a QComboBox based on theuserData
of 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.
Applications can also define additional mappings between type and auto-linking. To do this, simply add a new entry to the
HANDLERS
object:>>> echo.qt.autoconnect import HANDLERS >>> HANDLERS['color'] = connect_color
The handler function (
connect_color
in 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.