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

Qml Compiler: unresolved QStringBuilder results might cause problems

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P2: Important
    • 6.8.0 FF, 6.8
    • 6.7.0
    • QML: Compiler
    • None
    • ed61e7c6d (dev)

    Description

      Applications built with QT_USE_QSTRINGBUILDER might behave erratic with qmlcachegen generated C++ code if string concatenation is required. The resulting type will then be a QStringBuilder instead of QString, which might cause problems.

      The attached code illustrates such a problem. Here a QVariantMap with mimetype + data entries must be constructed for Drag&Drop. If the compiler is enabled and QT_USE_QSTRINGBUILDER is set, encoding of the string data will fail and this error is logged:

      Don't know how to encode variant of type QMetaType(QStringBuilder<QString,QString>) as mime type "text/plain"
      

      Workaround is to use an interim property for the concatination (coercing a string).

      import QtQuick
      
      Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
      
        Rectangle {
          id: dragSource
      
          width: 300
          height: 100
      
          property string modelData: "Drag Me"
      
          // workaround
          // readonly property string __modelData: "%" + modelData + "%"
      
          Text {
            id: parameterName
      
            anchors.centerIn: parent
            text: dragSource.modelData
          }
      
          Item {
            id: draggable
      
            Drag.active: dragArea.drag.active
            Drag.keys: [ "parameter" ]
            Drag.supportedActions: Qt.CopyAction
            Drag.proposedAction: Qt.CopyAction
            Drag.mimeData: {
              "text/plain": "%" + dragSource.modelData + "%"
              // "text/plain": dragSource.__modelData
            }
            Drag.dragType: Drag.Automatic
          }
      
          MouseArea {
            id: dragArea
            anchors.fill: parent
            hoverEnabled: true
            drag.target: draggable
            cursorShape: Qt.PointingHandCursor
            onPressed: dragSource.grabToImage(function(result) {
              draggable.Drag.imageSource = result.url
            })
          }
        }
      
        Rectangle {
          id: dragTarget
      
          x: 300
          width: 300
          height: 100
      
          Text {
            id: targetText
            anchors.centerIn: parent
            text: "Drop here"
          }
      
          DropArea {
            id: drop
            anchors.fill: parent
            keys: ["text/plain"]
            onDropped: function(drop) {
              console.log("DROP", drop, "hasTExt", drop.hasText, "Text", drop.text);
              if (drop.hasText) {
                targetText.text = drop.text;
                drop.accept(Qt.CopyAction);
              }
            }
          }
        }
      }
      

      CMakeLists.txt:

      target_compile_definitions(appuntitled12
        PUBLIC
          QT_USE_QSTRINGBUILDER
      )
      

      Attachments

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

        Activity

          People

            ulherman Ulf Hermann
            njeisecke_qtc Nils Jeisecke
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes