×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    Explanation on Dynamic Object Management in QML

    First of all, I just started to learn programming so a bit slow to understand it.

    There's this part that I don't seems to understand.

    createQmlObject ( string qml, object parent, string filepath )

    The "string filepath" as in the file path where my object will be created or something else?
    Because after try to practice how to do it, it doesn't work at all for me.

  2. #2
    Nokia Developer Moderator
    Join Date
    Sep 2004
    Location
    Tampere, Finland
    Posts
    11,359

    Re: Explanation on Dynamic Object Management in QML

    See http://doc.trolltech.com/4.7-snapsho...lObject-method

    If filepath is specified, it will be used for error reporting for the created object.
    A cryptic message but which basically means that the parameter matters not, and if for some reason your code does not work, the reason is elsewhere, not in this parameter.
    -- Lucian

    If you are not yet a DVLUP member it is time to correct that mistake :) Click here to join: http://www.dvlup.com/lucian/Invite

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    Re: Explanation on Dynamic Object Management in QML

    Here is the code just to do for testing. The result is it already appear a red rectangle when the simulator load rather than click the button to create.

    //---main.QML----//
    Code:
    import QtQuick 1.0
    import "componentCreation.js" as MyScript
    Rectangle {
        id: appWindow
        width: 300; height: 300
    
        Component.onCompleted: MyScript.createSpriteObjects();
    
        Rectangle{
            x: 22
            y: 207
            height:60
            width:40
            color: "grey"
    
        MouseArea{
        onClicked: Qt.createQmlObject("sprite.qml", appWindow,"object1") }
        }
    }
    //---sprite.qml---//
    Code:
    import QtQuick 1.0
    
     Rectangle{
         height: 60
         width: 40
         x: 180 * Math.random()
         y: 180 * Math.random()
         color: "red"
    }
    //---componentCreation.js---//
    Code:
    var component;
     var sprite;
    
     function createSpriteObjects() {
         component = Qt.createComponent("sprite.qml");
         if (component.status == Component.Ready)
             finishCreation();
         else
             component.statusChanged.connect(finishCreation);
     }
    
     function finishCreation() {
         if (component.status == Component.Ready) {
             sprite = component.createObject(appWindow);
             if (sprite == null) {
                 // Error Handling
             } else {
                 sprite.x = 100;
                 sprite.y = 100;
                 // ...
             }
         } else if (component.status == Component.Error) {
             // Error Handling
             console.log("Error loading component:", component.errorString());
         }
     }

  4. #4
    Nokia Developer Moderator
    Join Date
    Sep 2004
    Location
    Tampere, Finland
    Posts
    11,359

    Re: Explanation on Dynamic Object Management in QML

    You do not use createQmlObject to load a qml file. The QML code should be in the "string" parameter itself.

    Code:
    onClicked: Qt.createQmlObject('import QtQuick 1.0; Rectangle {height: 60; width: 60; color: "green";}', button2,"object1")
    As the documentation of createQmlObject says, if you are interested in loading components (e.g. loading new QML files) you should be using createComponent
    -- Lucian

    If you are not yet a DVLUP member it is time to correct that mistake :) Click here to join: http://www.dvlup.com/lucian/Invite

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    Re: Explanation on Dynamic Object Management in QML

    Can I have a simple example on createComponent code with button? It's quite a struggle to understand without basic on javascript

  6. #6
    Nokia Developer Moderator
    Join Date
    Sep 2004
    Location
    Tampere, Finland
    Posts
    11,359

    Re: Explanation on Dynamic Object Management in QML

    Here's a code snippet achieving the creation of a randomly positioned rectangle at a click of a button. It includes both a createQMLObject and a createComponent based solution.

    main.qml
    Code:
    import QtQuick 1.0
    
    Rectangle {
        width: 360
        height: 360
    
        Rectangle {
            id: button
            height: 60;
            width: parent.width
            color: "red"
    
            Text {
                text: "Exit"
                anchors.centerIn: parent
            }
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    Qt.quit();
                }
            }
        }
    
        Rectangle {
            id: button2
            anchors.top: button.bottom
    
            height: 60;
            width: parent.width
            color: "blue"
    
            function handleClick()
            {
                var component = Qt.createComponent("object1.qml");
                if (component.status == Component.Ready) {
                    var button = component.createObject(button2);
                    button.color = "red";
                }
    
            }
    
            Text {
                text: "Click for new square"
                anchors.centerIn: parent
            }
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    onClicked: parent.handleClick() //Qt.createQmlObject('import QtQuick 1.0; Rectangle {height: 60; width: 60; x: 180 * Math.random(); y: 180 * Math.random(); color: "red"; border.color: "black";}', button2,"object1")
                }
            }
        }
    
    }
    object1.qml
    Code:
    import QtQuick 1.0
    
    Rectangle {
        height: 60
        width: 60
        x: 180 * Math.random()
        y: 180 * Math.random()
        color: "blue"
        border.color: "black"
    }
    -- Lucian

    If you are not yet a DVLUP member it is time to correct that mistake :) Click here to join: http://www.dvlup.com/lucian/Invite

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    Re: Explanation on Dynamic Object Management in QML

    Thank you for the example.

    However, the createComponent is still not working whereas createQmlObject works. Instead it shows "error" in the console when I state "else {console.log("Error creating object")}

  8. #8
    Nokia Developer Moderator
    Join Date
    Sep 2004
    Location
    Tampere, Finland
    Posts
    11,359

    Re: Explanation on Dynamic Object Management in QML

    What is the error message you see?
    -- Lucian

    If you are not yet a DVLUP member it is time to correct that mistake :) Click here to join: http://www.dvlup.com/lucian/Invite

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    Re: Explanation on Dynamic Object Management in QML

    It's actually no error. But it doesn't create any component by using createComponent. The button is still functioning as it show "Error creating object" in the console log.

    Code:
    function handleClick()
            {
                var component = Qt.createComponent("object1.qml");
                if (component.status == Component.Ready) {
                    var button = component.createObject(button2);
                    button.color = "red";}
               else {console.log("Error creating object")}
                }

  10. #10
    Nokia Developer Moderator
    Join Date
    Sep 2004
    Location
    Tampere, Finland
    Posts
    11,359

    Re: Explanation on Dynamic Object Management in QML

    Have you created the "object1.qml" file in the same directory where main.qml is?
    The code above works fine for me, with or without the else branch.
    -- Lucian

    If you are not yet a DVLUP member it is time to correct that mistake :) Click here to join: http://www.dvlup.com/lucian/Invite

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    Re: Explanation on Dynamic Object Management in QML

    Hey it works! This is because object1.qml is not in the same folder with main.qml. Silly me :P

    Thanks alot!
    Last edited by VVNart; 2011-05-10 at 04:34.

Similar Threads

  1. QML: disconnect signal object from all slots from QML
    By semlanik in forum [Archived] Qt Quick
    Replies: 1
    Last Post: 2012-03-07, 02:39
  2. How to create and save object to put in another qml?
    By VVNart in forum [Archived] Qt Quick
    Replies: 4
    Last Post: 2011-05-08, 06:57
  3. Manipulating QML date object
    By jpgustaf in forum Qt
    Replies: 1
    Last Post: 2010-12-22, 09:15
  4. Can you store dynamic array's in a object?
    By kester76 in forum Symbian
    Replies: 6
    Last Post: 2006-12-21, 09:18

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×