Communication between two qml files



  • I want to update the textfield text in to the button text. The button and textfield are in different QML files, How can I achieve this?


  • Moderators

    @eswar There are several ways to do it for eg. accessing directly by id, signal and slots, javascript functions, alias's etc.. but that depends upon the scope of each of your QML elements. It would be better if you post a minimal code which will show in what context they are instantiated. This will help in suggesting a more suitable answer.



  • How can access the id from one qml file to another?


  • Moderators

    @eswar Did you instantiate the QML component present in 2nd file in 1st?



  • This post is deleted!

  • Moderators

    @eswar Try using alias. Have a look at its docs here. Declare a new alias in Page2.qml and point it to Label's text. Then when you instantiate Page2 where MyTextfield is you can access the alias defined in Page2 by its id. Something like:

    property alias labelText: lab_carearea.text
    
    MyTextfield {
       id: myTextField
    }
    
    Page2 {
       id: myPage
    }
    
    //access it when instantiated using id
    myPage.labelText = myTextField.text
    
    


  • @p3c0 still i cannot acess the textfield data from page1.qml to page2.qml. In that i want to display the page1.qml textfield entered data to display in the page2.qml . In the page1.qml contain more than 5 textfield. I need to display the tf _patient name textfield only in the page2.qml.


  • Moderators

    @eswar can you show what you tried ?



  • page1.qml

    item{
    property alias patientName: tf_patientname.tftext
    
     MyTextfield{
    
                id:tf_patientname
                tfheight: 50
                tfwidth: 250
                tffontbold: true
                tfborderradius: 5
                tfmaxmiumlength:25
                tfplaceholdertext:""
                tftextcolor:"#1B7497"
               tftext:""
               }
                }
    }
    

    Here 5 textfield like age, weight, dateof birth..I want to pass patient named data only to page2.qml

    How can i get the data from page1.qml
    i want to update the in listview.


  • Moderators

    @eswar So what error do you get when you try this ?



  • I didnt know how to acess data only in page2.qml

    if i give like

    page1{
    label{
    text:patientName
    }
    the textfield are arranged in row layout.

    error: ReferenceError: patientName is not defined

    if i give like this
    page1{
    }

    it will show all the same layout of page1 in page2. now i need from patient name text only in the page2..


  • Moderators

    @eswar Here is a small example. I have tried to adapt it to your requirements.

    //Page1.qml
    
    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Layouts 1.3
    
    Item {
        width: 200; height: 200
    
        ColumnLayout {
            anchors.fill: parent
            TextField {
                Layout.preferredWidth:  200
                Layout.preferredHeight: 40
                onTextChanged: page2.labelText = text
            }
    
            Page2 {
                id: page2
            }
        }
    }
    
    //Page2.qml
    
    import QtQuick 2.5
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.3
    
    Item {
        Layout.preferredWidth: 200
        Layout.preferredHeight: 40
        property alias labelText: label.text
    
        Label {
            id: label
            color: "red"
        }
    }
    

    Here when user types into the TextField the text is transferred to the Page2's Label via alias property. Perhaps you can try to do them same in your code.



  • The data is transferred to page1 to page2. In that i have face one problem. The two pages are appear in the tabview. The page2.qml label will be diaplayed in the page1.qml. How can i resolve this problem. I need only the textfield text is transferred to page 2.


  • Moderators

    @eswar I would suggest you to write a scale down version of your application and in that try what I suggested. If you already have then post that minimal complete example so that we can suggest the modifications there. That would be more faster.


Log in to reply
 

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