Forums

OverviewFelgo 2 Support (Qt 5) › dynamic creating object from *.qml's files.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #20532

    Kacper

    I want to create objects like AppButton or switch to SimpleRow in the AppListView depending of value in array.
    To create object I use Qt.createComponent in function giveMeComp(idx).
    I have error: ReferenceError: myItemId is not defined. I don’t know want why.

    My code:
    SimpleRowButton.qml

    import Felgo 3.0
    import QtQuick 2.9
    
    Component{
        SimpleRow {
            text: "name";
            AppButton{
                text: "aaaa"
                anchors.verticalCenter: parent.verticalCenter
                anchors.right: parent.right
                anchors.rightMargin: dp(10)
                Component.onCompleted: {
                    console.log(myIndex)
                    console.log("Przycisk")
                    //myIndex++;
                }
            }
    
            Component.onCompleted: {
                console.log("bbb"+switchsSettingsLogicArray[myIndex])
                //console.log(myIndex)
                myIndex++
            }
        }
    }
    

    SimpleRowSwitch:

    import Felgo 3.0
    import QtQuick 2.9
    
    Component{
        SimpleRow {
            text: "name";
            AppSwitch{
                anchors.verticalCenter: parent.verticalCenter
                anchors.right: parent.right
                anchors.rightMargin: dp(10)
                Component.onCompleted: {
                    console.log(myIndex)
                    if(switchsSettingsLogicArray[myIndex]===1){
                        checked=true
                    } else {
                        checked=false
                    }
                    //myIndex++;
                }
    
                onToggled: {
                    console.log(myIndex)
                }
            }
            Component.onCompleted: {
                console.log("a"+switchsSettingsLogicArray[myIndex])
                //console.log(myIndex)
                myIndex++
            }
        }
    }
    

    Main.qml:

    import Felgo 3.0
    import QtQuick 2.9
    import "Cr1.js" as MyScript
    
    App {
        //var appswitchs = {id: 1, value: false; id: 2, value: false};
        id: app
        property var switchsSettingsLogicArray: [0,0,1,1,1,0,0,1,1]
        property var typeOfElementsInSettings: ['switch','switch','button','switch','button','switch','switch','switch']
        property var firstRun
        property int myIndex: 0
    
        Navigation{
            NavigationItem{
                title: "Listy"
                icon: IconType.checkcircle
    
                NavigationStack{
                    Page{
                        id: task1
    
                        navigationBarTranslucency: 0.0
    
                        title: "Listy"
    
                        SearchBar {
                            id: searchBarThreads
                            onAccepted: {
    
                            }
                        }
                    }
    
                }
            }
    
            NavigationItem{
                title: "Konto u┼╝ytkownika"
                icon: IconType.user
    
                NavigationStack{
                    Page{
                        title: "Konto u┼╝ytkownia"
                    }
                }
            }
    
            NavigationItem{
                title: "Ustawienia"
                icon: IconType.cogs
    
                NavigationStack{
                    Page{
                        title: "Ustawienia"
    
    
    
                        /*ListView {
                            id: listme
                            spacing: 30;
                            model: [1,0,1]
                            delegate:comp
    
                            Component.onCompleted: {
                                console.log("gg")
                            }
                            width: 640
                            height: 100
                        }*/
    
                        AppListView{
                            id: listme
                            model: ListModel{
                                ListElement{
                                    type: "category 1"; name: "option 1"
                                }
                                ListElement{
                                    type: "category 1"; name: "option 2"
                                }
                                ListElement{
                                    type: "category 2"; name: "option 3"
                                }
                                ListElement{
                                    type: "category 2"; name: "option 4"
                                }
                            }
    
                            section.property: "type"
                            section.delegate: SimpleSection{
                                title: section
                                enabled: true
                                Component.onCompleted: {
                                    console.log("SimpleSection")
                                }
                            }
    
                            delegate: SimpleRow{
                                text: name
                                Component{
                                    id: comp
                                    Item {
                                        id: myItemId
                                        width: 200
                                        height: 100
    
    
                                        function giveMeComp(idx){
                                            var mycomponent;
                                            console.log(index)
                                            switch(idx){
                                            case 0:
                                                mycomponent = Qt.createComponent("SimpleRowButton.qml")
                                                if(mycomponent==null)console.log("Error creating component");
                                                break;
                                            case 1:
                                                mycomponent = Qt.createComponent("SimpleRowButton.qml")
                                                if(mycomponent==null)console.log("Error creating component");
                                                break;
                                            default :
                                                mycomponent = Qt.createComponent("SimpleRowButton.qml")
                                                if(mycomponent==null)console.log("Error creating component");
                                                break;
                                            }
                                            var obj = mycomponent.createObject(myItemId)
                                            if (obj == null) {
                                                console.log("Error creating object");
                                            }
                                            console.log(mycomponent)
                                            return mycomponent;
                                        }
    
    
                                    }
                                }
                                Component.onCompleted: {
                                    myItemId.giveMeComp(index+"aasa");
                                    console.log("I"+width)
                                    console.log("I"+height)
                                }
                            }
                        }
    
                    }
                }
                onSelected: {
                    //console.log("selected")
                }
                Component.onCompleted: {
                    //console.log("Zrobiono")
                }
            }
        }
        function init(){
            console.log("initialization...")
        }
    
        Component.onCompleted: {
            app.settings.clearAll()
            if(app.settings.getValue("firstRun") === undefined){
                console.log("firstRun: false")
                init()
                app.settings.setValue("firstRun", false)
    
            }
        }
    }
    

     

    Do you have any idea?

    #20537

    Alex
    Felgo Team

    Hi,

    this is because you wrapped the Item with “myItemId” in a Component, so it does not exist unless you create an object of it, thus also you cannot directly access the id of it. Remove the Component wrapper and you should be fine.

    Cheers,
    Alex

    #20539

    Kacper

    Ok, I did It. But now objects, which I create, do not instetting to my row. Are created but aren’t instetting and aren’t showing.

Viewing 3 posts - 1 through 3 (of 3 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support