Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-135364

Proposal: Add Decorators Support to QML for Enhanced Binding, Control Flow, and Property Behavior

    XMLWordPrintable

Details

    Description

      Summary:
      Introduce decorator support to QML syntax to enable modular extensions of properties and functions. This addresses recurring developer needs such as bi-directional bindings, custom getters/setters, debouncing of function calls, and value transformation — all in a declarative and maintainable way. Based on the ECMAScript Decorators Proposal (Stage 3).

       

      Description:
      QML benefits from a declarative syntax and JavaScript integration, but it lacks a first-class way to apply reusable logic to properties and functions without verbose boilerplate. This proposal introduces decorators to QML as a clean mechanism to attach behavior, metadata, and transformation logic in a modular way.

      Decorators are increasingly popular in modern JavaScript/TypeScript (via the [ECMAScript Decorators Proposal](https://github.com/tc39/proposal-decorators)) and align well with QML’s component model and reactive paradigm.

      Proposed Syntax (Hypothetical QML):

      import QtQuick 2.15
      import MyApp.Decorators 1.0
      Item {
      property alias sliderValue: slider.value
      @bindTwoWay("sliderValue")
      property int modelValue: 50
      @getter(function()
      { return _internal * 2; }
      )
      @setter(function(val)
      { _internal = val / 2; }
      )
      property int doubled
      property int _internal: 10
      @transform(function(val)
      { return val.trim().toLowerCase(); }
      )
      property string username: "User123 "
      @debounce(300)
      function updateLayout()
      { console.log("Expensive layout updated"); }
      onWidthChanged: updateLayout()
      }
      

       

      Key Use Cases:

      • Bi-Directional Binding (@bindTwoWay)
        Keeps two properties (e.g. model ↔ UI) in sync. Highly requested by the QML community.
      • Custom Getters/Setters (@getter, @setter)
        Enables computed properties and encapsulated logic without clutter.
      • Value Transformation (@transform)
        Automatically formats or normalizes values, e.g. trimming strings or clamping numbers.
      • Debounced Function Calls (@debounce)
        Improves performance for high-frequency triggers like `onTextChanged`, `onWidthChanged`, etc.
      • Validation (@validate)
        Enforces runtime constraints such as ranges or patterns.
      • Debugging (@logAccess, @trace)
        Adds automatic logging for diagnostics and development ease.

       

      Why Now?

      • Decorators solve multiple highly requested features with a consistent syntax.
      • Aligns QML with modern JavaScript/TypeScript development patterns.
      • Keeps QML declarative while enabling modular, reusable behavior.

       

      Relevant Specification:

       

      Implementation Considerations:

      • Can be implemented as syntactic sugar or runtime wrappers.
      • Decorators may be defined in JS (as functions) or exposed via QML modules.
      • Must remain fully backward-compatible and opt-in.

       

      Benefits:

      • Reduces boilerplate and increases expressiveness.
      • Encourages reusable, declarative design patterns.
      • Offers clean solutions to common UI + data synchronization challenges.
      • Brings QML closer to the modern JavaScript ecosystem.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qtqmlteam Qt Qml Team User
            prnogaj Przemyslaw Nogaj
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes