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

Write interceptors for properties

    XMLWordPrintable

Details

    Description

      Writing types in C++, it is possible to do interesting things with property setters – for example, clamping values to a range. This is difficult to do from QML – we can't detect value changes until after the fact, and changing a value may remove a binding.

      It isn't 100% clear how widely this should be encouraged – for example the existing visible property has led to issues because the value you set and the value you get are not identical (you set local visibility and get inherited visibility). Write interception might lead to similar confusion if not used carefully.

      Idea 1:

      WriteInterceptor on x {
          id: interceptor
          setter: function(newValue) {
              return Math.max(maxValue, Math.min(minValue, newValue))
          }
      }
      

      Pros: Basic implementation very easy
      Cons: Doesn't play nice with Behavior (only one will be called). Requires a QObject for each property that needs interception.

      Idea 2:

      function[interceptor:x] setX(newValue) {
          x = Math.max(maxValue, Math.min(minValue, newValue))
      }
      

      Attachments

        Issue Links

          For Gerrit Dashboard: QTBUG-59761
          # Subject Branch Project Status CR V

          Activity

            People

              qtqmlteam Qt Qml Team User
              mbrasser Michael Brasser
              Votes:
              6 Vote for this issue
              Watchers:
              15 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There is 1 open Gerrit change