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

Inconsistent binding behavior

    XMLWordPrintable

Details

    Description

      Sometimes, it is inevitable that we might need to explicitly capture extra properties in a binding:

      import QtQuick
      import QtQuick.Window
      
      Window {
          id: window
          width: 320
          height: 320
          visible: true
      
          readonly property var globalPos: {
              window.x;
              window.y;
              return contentItem.mapToGlobal(0, 0);
          }
      
          onGlobalPosChanged: console.log(globalPos.x, globalPos.y)
      } 

      Codes above work well with Qt 5, while on Qt 6 the globalPos property no longer get evaluated when moving the window.

      Interestingly, we can workaround this issue with these magic:

           readonly property point globalPos: { // only changing property type
              window.x;
              window.y;
              return contentItem.mapToGlobal(0, 0);
          }

      or

           readonly property var globalPos: {
              window.x;
              window.y;
              const p = contentItem.mapToGlobal(0, 0);
              return { x: p.x, y: p.y };
          }

      or less magic

          function unused_reference() {}
      
           readonly property point globalPos: { // only changing property type
              unused_reference(window.x, window.y);
              return contentItem.mapToGlobal(0, 0);
          }

      Does explicitly capturing syntax still work with Qt 6? Or should such trick never be used on any versions of Qt?

      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
            gpbeta Joshua GPBeta
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes