Load a qml when button click



  • Hey. I need to load a graph.qml in main.qml and need to pass some value to from main.qml, so just created a rectangle and creating object of graph.qml and when i click on mouse (in main.qml) I am calling fucntion from graph.qml

    like this mail.qml
    Rectangle {
    id: graphView
    x: 0
    y: 200
    width: 630
    height: 443
    gradient: Gradient {
    GradientStop {
    position: 0
    color: "#ffffff"
    }

                GradientStop {
                    position: 1
                    color: "#000000"
                }
            }
    
           Graph
           {
               id:graphChart
           }
    

    calling function here
    MouseArea
    {
    anchors.fill: parent

                onClicked:
                {
    
                    console.log(qsTr("Connect Button Clicked"))// + textEdit.text + '"'))
                   // loader.source = "Graph.qml"
                    graphChart.callme("Temprature","true")
                    console.log(qsTr(myc.connectAurdino("Hello") +"from qml"))
    
                    //To-do
    
                    //Connection with aurdino will come here
                  
                }
    
    
            }
    

    graph.qml

    Item {
    id: graph
    anchors.fill: parent
    function callme(val1,val2)
    {
    console.log("I am called = "+val1+" =ok= "+val2)
    }
    ChartView {
    id:chartName
    title: "Sensor"
    anchors.fill: parent
    antialiasing: true

        LineSeries {
            id:temperature
            name: val1
            XYPoint { x: 0; y: 0 }
            XYPoint { x: 1.1; y: 2.1 }
            XYPoint { x: 1.9; y: 3.3 }
            XYPoint { x: 2.1; y: 2.1 }
            XYPoint { x: 2.9; y: 4.9 }
            XYPoint { x: 3.4; y: 3.0 }
            XYPoint { x: 4.1; y: 3.3 }
    
    
        }
    

    but I am getting error because graph load before calling fucntion and doesnt find variable which i am assging in graph.qml so it gives refrence error.



  • Define one custom property in Graph.qml
    property int val : 1000

    Set the value of this variable when you are creating Graph.qml object



  • @dheerendra it worked.



  • @dheerendra I need to load graph.qml when button click, I changed according to you but right now I am displaying graph in rectangle so when application start it load, but i need to load in button click



  • @dheerendra I did according to you and it works, but the main concern is I need to load graph.qml when click on button (Load graph)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.