Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. createQmlObject using comments

createQmlObject using comments

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Valerian
    wrote on last edited by Valerian
    #1

    Hi All,

    I'm trying to create dynamic components using Qt.createQmlObject

    Qt.createQmlObject(text, contentRect, "dynamicItem")
    

    However, the text needs to be single line.

    'import QtQuick 2.0;Rectangle{ width: parent.width; contentForReplace}'
    

    The contentForReplace is replaced dynamically to create different components at runtime

    Is it possible to have // or /* comments */ in the single line?

    Kindly advice

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @Valerian

      However, the text needs to be single line.

      No. Why? Doesn't matter till it is a valid string.

      The contentForReplace is replaced dynamically to create different components at runtime

      Do you mean contentForReplace is some sort of variable which will append string to that exsiting string ?

      Is it possible to have // or /* comments */ in the single line?

      Yes. Until it stays a valid string.

      157

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Valerian
        wrote on last edited by
        #3

        @p3c0 thanks for the feedback.

        In the example for "object createQmlObject(string qml, object parent, string filepath)", it's mentioned as

        var newObject = Qt.createQmlObject('import QtQuick 2.0; Rectangle {color: "red"; width: 20; height: 20}',
                                             parentItem,
                                             "dynamicSnippet1");
        

        Where the string qml is a single line, where each line is seperated with a semi-colon

        contentForReplace can be replaced with another qml Rectangle object or qml TextEdit object, etc..

        If my contentForReplace has "//" , the entire string becomes as comment and the error reported is
        "qrc:/qml/main.qml:146: Error: Qt.createQmlObject(): failed to create object:
        qrc:/qml/dynamicItem:1:200: Expected token `}'"

        If my contentForReplace has "/* Test comment */", it fails to create an object with the following error
        "qrc:/qml/main.qml:146: Error: Qt.createQmlObject(): failed to create object:
        qrc:/qml/dynamicItem:1:200: Unexpected token `;'"

        Kindly advice

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @Valerian

          Where the string qml is a single line, where each line is seperated with a semi-colon

          Yes. Multiple properties on same line are separated by a semicolon.

          If my contentForReplace has "//" , the entire string becomes as comment and the error reported is ...

          That is because the parser could not find the end of a comment. If you are using a single line use /* */ for commenting.

          Also it doesnt have to be in single line compulsarily. Its that it just saves some lines.
          You can also write it is:

          var newObject = Qt.createQmlObject("import QtQuick 2.0
                                               Rectangle {
                                                  color: \"red\"
                                                  width: 20
                                                  height: 20
                                              }", parentItem, 
                                           "dynamicSnippet1");
          

          This is more easily understandable.

          157

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Valerian
            wrote on last edited by
            #5

            Thanks @p3c0

            I have a QML TextArea, where a user can type the qml to be shown.

            To display this qml written using createQmlObject, I have to run the following checks

            • replace \n with ;

            • replace \t with empty char

            The problem I am facing if a user types /* */ in the qml to be displayed, I need to parse it and find the first and last occurence which can be tedious

            Kindly advice

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @Valerian
              Why do you want to replace those characters ? As said earlier it doesnot need to be in single line.
              Since you are using TextArea you can just get the text using text property and use it directly. It will work.

              157

              1 Reply Last reply
              1
              • V Offline
                V Offline
                Valerian
                wrote on last edited by
                #7

                @p3c0 Thanks for the input, it worked

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved