Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Communication between two qml files
Qt 6.11 is out! See what's new in the release blog

Communication between two qml files

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 2 Posters 6.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eswar
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @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.

      157

      1 Reply Last reply
      1
      • E Offline
        E Offline
        eswar
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

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

          157

          1 Reply Last reply
          1
          • E Offline
            E Offline
            eswar
            wrote on last edited by eswar
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @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
              
              

              157

              1 Reply Last reply
              0
              • E Offline
                E Offline
                eswar
                wrote on last edited by
                #7

                @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.

                1 Reply Last reply
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  @eswar can you show what you tried ?

                  157

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    eswar
                    wrote on last edited by p3c0
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on last edited by
                      #10

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

                      157

                      1 Reply Last reply
                      0
                      • E Offline
                        E Offline
                        eswar
                        wrote on last edited by eswar
                        #11

                        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..

                        1 Reply Last reply
                        0
                        • p3c0P Offline
                          p3c0P Offline
                          p3c0
                          Moderators
                          wrote on last edited by
                          #12

                          @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.

                          157

                          1 Reply Last reply
                          0
                          • E Offline
                            E Offline
                            eswar
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0
                            • p3c0P Offline
                              p3c0P Offline
                              p3c0
                              Moderators
                              wrote on last edited by
                              #14

                              @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.

                              157

                              1 Reply Last reply
                              1

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved