how to recieve/send text from one qml file to another qml file



  • i want to send the textfield text from main.qml to search.qml file

    main.qml snippet

     TextField{
                       id: search_text
                       placeholderText: " Search "
                       width: drawer.width
    
    
    
                       Image {
                           id: search_button
                           anchors.right: search_text.right
                           anchors.verticalCenter: search_text.verticalCenter
                           source: "qrc:/images/search.png"
                           MouseArea{
                               anchors.fill: search_button
                               onClicked: {
                                  loader.source = "qrc:/pages/search.qml"
                                   stackView.push(loader.item)
                                   listView.currentIndex = -1
                               }
    
    
    
                           }
                       }
    
                   }
    

    search.qml

    import QtQuick 2.6
    import QtQuick.Controls 2.0
    import QtWebView 1.1
    
    Pane {
        id: pane
    
    
    // recieve texinput here so that i can construct url
    
        WebView{
            id:webview
            anchors.fill: parent
    
            Component.onCompleted: url = "http://en.wikitolearn.org/"
        }
    }
    
    

  • Moderators

    @Qjay You can create a property in Search.qml and access this property through Loader.
    For eg.

    loader.item.myProperty //myProperty is defined in Search.qml
    


  • @p3c0 , i tried this

     TextField{
                        id: search_text
                        placeholderText: " Search WikiToLearn"
                        width: drawer.width
                        onAccepted: {
                            loader.item.search = search_text.text
                            loader.source = "qrc:/pages/search.qml"
                            stackView.push(loader.item)
                            listView.currentIndex = -1
    
    
                        }
    
    

    error :

    qrc:/main.qml:104: TypeError: Type error
    qrc:/main.qml:104: TypeError: Type error
    


  • @Qjay
    You can get text like this,

    1. Create instance of search.qml means like
      Search(this is your search.qml file instance, first latter should be capital of your qml file)
      {
      id: searchText
      }

    2 In search.qml file create property like
    property alias Name_of_variable(like textOfSearchField): id_of_textfield(search_text.text)

    1. Now wherever you want to use, can do like this.
      searchText(id_of_instance).Name_of_variable

    You will find exact value as you passed in search.qml file



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