QML text inpu to another QML text



  • I have 2 Qml's, QML1 contains a textinput where you can write texts, on QML2 i have a text

    Q> How can i show the written text in qml1 to qml2?



  • Again when you say qml1 and qml2, are they different views, or just elements in a given view. If you can post some code with what you want to do, it will be easy..



  • qml2 is a new screen



  • @//this is QML1
    Rectangle {
    width: 360
    height: 360

    Loader {
    id: mainLoader
    anchors.fill: parent
    
    TextInput {
        id: text_input1
        x: 117
        y: 104
        width: 183
        height: 35
        text: ""
        font.bold: true
        font.pixelSize: 19
    }
    
    MouseArea {
        id: ok
        x: 124
        y: 184
        width: 100
        height: 89
        onClicked:  { mainLoader.source = "main2.qml";}
    }
    
    Text {
        id: text1
        x: 158
        y: 215
        width: 80
        height: 20
        text: "OK"
        font.bold: true
        font.pixelSize: 22
    }
    

    }

    Text {
    id: text2
    x: 23
    y: 116
    width: 80
    height: 20
    text: "Write here:"
    font.bold: true
    font.pixelSize: 14
    }
    }
    @

    @import QtQuick 1.0

    //this QML2

    Rectangle {
    width: 340
    height: 399

    Text {
        id: text1
        x: 16
        y: 124
        width: 80
        height: 20
        text: "The text i wrote from QML1:"
        font.bold: true
        font.pixelSize: 19
    }
    
    Text {
        id: textpreview
        x: 150
        y: 177
        width: 89
        height: 47
        text: "text"
        font.bold: true
        font.pixelSize: 20
    }
    

    }
    @

    When i pressed the mousearea ok, the text written in qml1 will be previwed automatically in qml2



  • This is how it can be done

    main.qml
    @import QtQuick 1.0

    Rectangle {
    id: mainView
    anchors.fill : parent
    QML1 {
    id:qml1
    anchors.fill: parent
    }
    }
    @

    QML1.qml
    @import QtQuick 1.0

    //this is QML1
    Rectangle {
    width: 360
    height: 360

    Loader {
        id: mainLoader
        anchors.fill: parent
        
        TextInput {
            id: text_input1
            x: 117
            y: 104
            width: 183
            height: 35
            text: ""
            font.bold: true
            font.pixelSize: 19
        }
        
        MouseArea {
            id: ok
            x: 124
            y: 184
            width: 100
            height: 89
            onClicked:  {
                mainLoader.source = "QML2.qml";
                mainLoader.item.textPosted(text_input1.text)
            }
        }
        
        Text {
            id: text1
            x: 158
            y: 215
            width: 80
            height: 20
            text: "OK"
            font.bold: true
            font.pixelSize: 22
        }
    }
    
    Text {
        id: text2
        x: 23
        y: 116
        width: 80
        height: 20
        text: "Write here:"
        font.bold: true
        font.pixelSize: 14
    }
    

    }
    @

    QML2.qml
    @import QtQuick 1.0

    //this QML2

    Rectangle {
    width: 340
    height: 399

    signal textPosted(string textFromQml1)
    
    onTextPosted: text1.text = textFromQml1
    Text {
        id: text1
        x: 16
        y: 124
        width: 80
        height: 20
        //text:
        font.bold: true
        font.pixelSize: 19
    }
    
    Text {
        id: textpreview
        x: 150
        y: 177
        width: 89
        height: 47
        text: "text"
        font.bold: true
        font.pixelSize: 20
    }
    

    }
    @



  • You can also take the advantages of Connections element:

    QML1.qml
    @import QtQuick 1.0
    //this is QML1
    Rectangle {

    id: qml1Text
    width: 360
    height: 360
    
    signal textPosted(string textFromQml1)
    
    Loader {
        id: mainLoader
        anchors.fill: parent
        TextInput {
            id: text_input1
            x: 117
            y: 104
            width: 183
            height: 35
            text: "sddddddd"
            font.bold: true
            font.pixelSize: 19
        }
    
        Rectangle {
            x: 124
            y: 184
            width: 100
            height: 89
            color: "lightblue"
        }
        MouseArea {
            id: ok
            x: 124
            y: 184
            width: 100
            height: 89
    
            onClicked:
            {
                mainLoader.source = "QML2.qml";
                qml1Text.textPosted(text_input1.text)
            }
        }
    
        Text {
            id: text1
            x: 158
            y: 215
            width: 80
            height: 20
            text: "OK"
            font.bold: true
            font.pixelSize: 22
        }
    }
    Text {
        id: text2
        x: 23
        y: 116
        width: 80
        height: 20
        text: "Write here:"
        font.bold: true
        font.pixelSize: 14
    }
    

    }
    @

    QML2.qml
    @import QtQuick 1.0
    //this QML2
    Rectangle {
    width: 340
    height: 399
    Connections {
    target: qml1Text
    onTextPosted: text1.text = textFromQml1
    }

    Text {
        id: text1
        x: 16
        y: 124
        width: 80
        height: 20
        //text: "The text i wrote from QML1:"
        font.bold: true
        font.pixelSize: 19
    }
    Text {
        id: textpreview
        x: 150
        y: 177
        width: 89
        height: 47
        text: "text"
        font.bold: true
        font.pixelSize: 20
    }
    

    }
    @



  • thanks from me 2, i am also looking for that snippet



  • Thanks ^^


Log in to reply
 

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