Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 1 Support › Accessing Properties

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #7178

    Preston

    Hi,

    The best way to ask my question is to first present the code:

    // import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.1
    import VPlay 1.0
    
    SceneBase {
    
        property bool onOrOff
        onOnOrOffChanged: {
            toggleSplBtn.color = (onOrOff == true) ? "red" : "darkblue"
        }
    
        Sound {
            id: soundChosen
        }
    
        SimpleButton {
            id: toggleSplBtn
            anchors.top: parent.top
            anchors.right: parent.right
    
            text: "Spell"
    
            Component.onCompleted: {
                if(onOrOff == true){
                    toggleSplBtn.color = "red"
                }else{
                    toggleSplBtn.color = "darkblue"
                }
            }
    
            onClicked: {
                onOrOff = !onOrOff
                console.debug(onOrOff)
            }
        }
    
        SimpleButton {
            anchors.left: parent.left
            anchors.bottom: parent.bottom
    
            text: "Back"
    
            onClicked: {
                console.debug("BACK TO MAIN MENU")
                window.state = "menu"
            }
        }
    
        Scene {
            id: movableScene
    
            MultiTouchArea {
                //anchors.fill: parent
                width: parent.width
                height: parent.height
    
                //NOTE: I want to set height above based on the calculation below
    
                enabled: true
                multiTouch.target: parent
                multiTouch.dragAxis: MultiTouch.YAxis
                multiTouch.maximumY: 0
                onPressed: {
                    console.debug("multi touch for movableScene")
                }
            }
    
            EntityManager {
                id: entityManager
                entityContainer: movableScene
            }
    
            property url jsonFile: "json/ThirtyConsonantsWithVowels.json"
    
            Component {
                    id: btnComp
    
                    EntityBase {
    
                        id: entityBaseX
    
                        property int index: 1
                        property string character
                        property double spacing: 50
                        property double widthValue: 40
                        property double heightValue: 40
                        property double numOfCol: 8
                        property double widthOfSet: numOfCol * spacing
    
                        entityId: index
                        entityType: "buttonEntity"
                        SimpleButton {
                            id: btn
    
                            property int col: index % numOfCol + 1
                            property int row: Math.floor(index / numOfCol) + 1
    
                            x: col * spacing + (parent.parent.width -  widthOfSet)/2 - spacing + (spacing - widthValue)/2
                            y: row * spacing
    
                            width: widthValue
                            height: heightValue
    
                            //text: col + "/" + row
                            text: character
    
                            onClicked: {
                                console.debug("button " + btn.text + " clicked")
                                if(onOrOff){
                                    soundChosen.source = "snd/" + btn.text + "-complete" + ".wav"
                                    soundChosen.play()
    
                                }else{
                                    soundChosen.source = "snd/" + btn.text + ".wav"
                                    soundChosen.play()
    
                                }
                                console.debug(soundChosen.source)
                            }
                        }
                        Component.onCompleted: console.debug("btn created " + index)
                    }
            }
    
            Component.onCompleted: {
                console.debug("consonantsPlusVowelsScene - Component.onCompleted")
                console.debug(jsonFile)
                console.debug("inside consonantsPlusVowels")
                var data = fileUtils.readFile(jsonFile)
                console.debug(data)
    
                var jsonObj = JSON.parse(data)
                console.debug("jsonObj")
                console.debug(jsonObj)
    
                for(var i = 0; i < jsonObj.length; i++)
                {
                    console.debug(jsonObj[i])
                    entityManager.createEntityFromComponentWithProperties(btnComp, {index: i, character: jsonObj[i]})
                    console.debug("create button")
                }
    
    
    
              
                //console.debug("entityBaseX.numOfCol")
                //console.debug(entityBaseX.numOfCol)
                //var numOfRow = jsonObj.length / entityBaseX.numOfCol
                //console.debug("numOfRow is below " + numOfRow)
                //console.debug(numOfRow)
                //movableScene.height = numOfRow * btn.spacing
                //movableScene.height = 100
            }
        }
    }
    

     

    If you look at the commented lines above:

    //console.debug("entityBaseX.numOfCol")
                //console.debug(entityBaseX.numOfCol)
                //var numOfRow = jsonObj.length / entityBaseX.numOfCol
                //console.debug("numOfRow is below " + numOfRow)
                //console.debug(numOfRow)
                //movableScene.height = numOfRow * btn.spacing
                //movableScene.height = 100

    You will see that I am trying to access the property called “numOfCol” of EntityBase with id of entityBaseX.

    The problem is, this does not work.

    I am getting this error:

    <!–StartFragment–>file:///Users/bliss/Coder/Felgo/TP-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Debug/TP.app/Contents/Resources/qml/ConsonantsPlusVowelsScene.qml:148: ReferenceError: Can’t find variable: entityBaseX

    EntityManager: onCompleted()<!–EndFragment–>

     

    So how do I access the property numOfCol in this case???

     

    Thank you very much…..

     

     

     

    #7179

    Christian
    Felgo Team

    Hi bliss,

    nice to see you’re working full steam on your project 🙂

    To your question, your code would be correct but the thing is, you can’t access an element inside a Component from the outside. This is because Components describe the structure of elements that will be created in the future and do not exist yet.

    So to access properties of elements created from Components with EntityManager, you need to access them via entityManager.getEntityById().

    Your code could look like this:

    
    //find entity with ID == 1
    var entity = entityManager.getEntityById(1);
    
    //access entity property
    var numOfRow = jsonObj.length / entity.numOfCol;

    Hope this helps!

    Cheers, Chrisu

    • This reply was modified 10 years, 1 month ago by  Chrisu.
Viewing 2 posts - 1 through 2 (of 2 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded