:right-sidebar: True Binding =================================================================== .. currentmodule:: gi.repository.GObject .. versionadded:: 2.26 .. class:: Binding(**properties: ~typing.Any) :no-contents-entry: Superclasses: :class:`~gi.repository.GObject.Object` ``GObject`` instance (or source) and another property on another ``GObject`` instance (or target). Whenever the source property changes, the same value is applied to the target property; for instance, the following binding: .. code-block:: c :dedent: g_object_bind_property (object1, "property-a", object2, "property-b", G_BINDING_DEFAULT); will cause the property named "property-b" of ``object2`` to be updated every time :obj:`~gi.repository.GObject.set` or the specific accessor changes the value of the property "property-a" of ``object1``\. It is possible to create a bidirectional binding between two properties of two ``GObject`` instances, so that if either property changes, the other is updated as well, for instance: .. code-block:: c :dedent: g_object_bind_property (object1, "property-a", object2, "property-b", G_BINDING_BIDIRECTIONAL); will keep the two properties in sync. It is also possible to set a custom transformation function (in both directions, in case of a bidirectional binding) to apply a custom transformation from the source value to the target value before applying it; for instance, the following binding: .. code-block:: c :dedent: g_object_bind_property_full (adjustment1, "value", adjustment2, "value", G_BINDING_BIDIRECTIONAL, celsius_to_fahrenheit, fahrenheit_to_celsius, NULL, NULL); will keep the "value" property of the two adjustments in sync; the ``celsius_to_fahrenheit`` function will be called whenever the "value" property of ``adjustment1`` changes and will transform the current value of the property before applying it to the "value" property of ``adjustment2``\. Vice versa, the ``fahrenheit_to_celsius`` function will be called whenever the "value" property of ``adjustment2`` changes, and will transform the current value of the property before applying it to the "value" property of ``adjustment1``\. Note that :obj:`~gi.repository.GObject.Binding` does not resolve cycles by itself; a cycle like .. code-block:: :dedent: object1:propertyA -> object2:propertyB object2:propertyB -> object3:propertyC object3:propertyC -> object1:propertyA might lead to an infinite loop. The loop, in this particular case, can be avoided if the objects emit the ``GObject::notify`` signal only if the value has effectively been changed. A binding is implemented using the ``GObject::notify`` signal, so it is susceptible to all the various ways of blocking a signal emission, like :obj:`~gi.repository.GObject.signal_stop_emission` or :obj:`~gi.repository.GObject.signal_handler_block`\. A binding will be severed, and the resources it allocates freed, whenever either one of the ``GObject`` instances it refers to are finalized, or when the :obj:`~gi.repository.GObject.Binding` instance loses its last reference. Bindings for languages with garbage collection can use :obj:`~gi.repository.GObject.Binding.unbind` to explicitly release a binding between the source and target properties, instead of relying on the last reference on the binding, source, and target instances to drop. Methods ------- .. rst-class:: interim-class .. class:: Binding :no-index: .. method:: dup_source() -> ~gi.repository.GObject.Object | None Retrieves the :obj:`~gi.repository.GObject.Object` instance used as the source of the binding. A :obj:`~gi.repository.GObject.Binding` can outlive the source :obj:`~gi.repository.GObject.Object` as the binding does not hold a strong reference to the source. If the source is destroyed before the binding then this function will return :const:`None`. .. versionadded:: 2.68 .. method:: dup_target() -> ~gi.repository.GObject.Object | None Retrieves the :obj:`~gi.repository.GObject.Object` instance used as the target of the binding. A :obj:`~gi.repository.GObject.Binding` can outlive the target :obj:`~gi.repository.GObject.Object` as the binding does not hold a strong reference to the target. If the target is destroyed before the binding then this function will return :const:`None`. .. versionadded:: 2.68 .. method:: get_flags() -> ~gi.repository.GObject.BindingFlags Retrieves the flags passed when constructing the :obj:`~gi.repository.GObject.Binding`\. .. versionadded:: 2.26 .. method:: get_source() -> ~gi.repository.GObject.Object | None Retrieves the :obj:`~gi.repository.GObject.Object` instance used as the source of the binding. A :obj:`~gi.repository.GObject.Binding` can outlive the source :obj:`~gi.repository.GObject.Object` as the binding does not hold a strong reference to the source. If the source is destroyed before the binding then this function will return :const:`None`. Use :func:`~gi.repository.GObject.Binding.dup_source` if the source or binding are used from different threads as otherwise the pointer returned from this function might become invalid if the source is finalized from another thread in the meantime. .. versionadded:: 2.26 .. deprecated:: 2.68 Use :func:`~gi.repository.GObject.Binding.dup_source` for a safer version of this function. .. method:: get_source_property() -> str Retrieves the name of the property of :obj:`~gi.repository.GObject.Binding`\:source used as the source of the binding. .. versionadded:: 2.26 .. method:: get_target() -> ~gi.repository.GObject.Object | None Retrieves the :obj:`~gi.repository.GObject.Object` instance used as the target of the binding. A :obj:`~gi.repository.GObject.Binding` can outlive the target :obj:`~gi.repository.GObject.Object` as the binding does not hold a strong reference to the target. If the target is destroyed before the binding then this function will return :const:`None`. Use :func:`~gi.repository.GObject.Binding.dup_target` if the target or binding are used from different threads as otherwise the pointer returned from this function might become invalid if the target is finalized from another thread in the meantime. .. versionadded:: 2.26 .. deprecated:: 2.68 Use :func:`~gi.repository.GObject.Binding.dup_target` for a safer version of this function. .. method:: get_target_property() -> str Retrieves the name of the property of :obj:`~gi.repository.GObject.Binding`\:target used as the target of the binding. .. versionadded:: 2.26 .. method:: unbind() -> None Explicitly releases the binding between the source and the target property expressed by ``binding``\. This function will release the reference that is being held on the ``binding`` instance if the binding is still bound; if you want to hold on to the :obj:`~gi.repository.GObject.Binding` instance after calling :func:`~gi.repository.GObject.Binding.unbind`, you will need to hold a reference to it. Note however that this function does not take ownership of ``binding``\, it only unrefs the reference that was initially created by :func:`~gi.repository.GObject.Object.bind_property` and is owned by the binding. .. versionadded:: 2.38 Properties ---------- .. rst-class:: interim-class .. class:: Binding :no-index: .. attribute:: props.flags :type: ~gi.repository.GObject.BindingFlags The type of the None singleton. .. versionadded:: 2.26 .. attribute:: props.source :type: ~gi.repository.GObject.Object The type of the None singleton. .. versionadded:: 2.26 .. attribute:: props.source_property :type: str The type of the None singleton. .. versionadded:: 2.26 .. attribute:: props.target :type: ~gi.repository.GObject.Object The type of the None singleton. .. versionadded:: 2.26 .. attribute:: props.target_property :type: str The type of the None singleton. .. versionadded:: 2.26