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. Unable to create a new object in Qml/javascript

Unable to create a new object in Qml/javascript

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 3 Posters 915 Views
  • 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.
  • C Offline
    C Offline
    Cyrille de Brebisson
    wrote on last edited by
    #1

    Hello,

    I connect a function to a signal on an object at component creation like so.
    In reaction to the event, I am trying to create an object and place it in a varaible...
    However, this does not seem to work as I get and "TypeError: Type error" error at run time on the var m2= new obj2(); obj2 is of course registered with qml.
    I tried it with var rr= new Rectangle(); but got the same result...

        Component.onCompleted: {
            obj.onSomething.connect(function() {
                    console.log("here"); // This works well...
                    var m2= new obj2();
    

    Any clue what is going wrong?

    Thanks

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Are you sure "new" should be used and not createObject ?

      http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Cyrille de Brebisson
        wrote on last edited by
        #3

        Hello,

        I honestly do not know as I am new to Qml...

        obj2 is a C++ class and I want to get a new instance of it to use in the rest of the rest of my function.

        How would I use createObject in this case?

        Cyrille

        mrjjM 1 Reply Last reply
        0
        • C Cyrille de Brebisson

          Hello,

          I honestly do not know as I am new to Qml...

          obj2 is a C++ class and I want to get a new instance of it to use in the rest of the rest of my function.

          How would I use createObject in this case?

          Cyrille

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Cyrille-de-Brebisson
          Hi
          Im also a new to QML so i might not be the best to answer.
          However, it does sound like this case
          http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html
          and still using createObject to get a new (c++) typein QML.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Cyrille de Brebisson
            wrote on last edited by
            #5

            Hello,

            In the example, the "BackEnd" object is statically created in Qml, not dynamically done...
            So, I do not think that it will be useful here :-(.

            Cyrille

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              Did you get a chance to look "Dynamic QML Object Creation from JavaScript" in qml. Documentation has more details on how to do it. @mrjj is pointing your towards that direction.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              1
              • C Offline
                C Offline
                Cyrille de Brebisson
                wrote on last edited by
                #7

                Hello,

                The following seems to work:
                var m2 = Qt.createQmlObject('import HPACPinkyQml 1.0; HPACPinkyQml { id: test; linkId:'+linkId+'}', pagesModel, "myfile");

                But it seems weired that I have to pass text that then needs to be parsed to do this... Especially as it makes passing "arguement" that are more complex than integers and the like quite tricky...

                Is there no way to do the same thing without going through the compiler?

                Cyrille

                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