Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to keep a input state after I click a button
Forum Updated to NodeBB v4.3 + New Features

How to keep a input state after I click a button

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 4 Posters 440 Views 1 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.
  • M Offline
    M Offline
    morita
    wrote on 24 Nov 2020, 11:48 last edited by
    #1

    Hello.

    I would like to keep a input state in the "TextInput" after I click a button.

    If I clicked the "TextInput" and clicked the button, the input state was lost.

    I don't know how to solve it.

    Item {
        id: texInp
        anchors.left: parent.left
        anchors.leftMargin: 50
        anchors.verticalCenter: parent.verticalCenter
    
        Row {
            spacing: 10
    
            //テキスト追加
            Button {
                id:tBtn
                height: 50
                width: 50
                text: "A"
                onClicked: {
                    switch (myCheck) {
                    case 1:
                        inpA.text = tBtn.text
                        break;
                    case 2:
                        inpB.text = tBtn.text
                        break;
                    default:
                        break;
                    }
                    myCheck = 0
                }
            }
    
            //一文字削除
            Button {
                id:aBtn
                height: 50
                width: 50
                text: "Del"
                onClicked: {
                    switch (myCheck) {
                    case 1:
                        TextDel.fnDelBtn(inpA.text)
                        break;
                    case 2:
                        TextDel.fnDelBtn(inpB.text)
                        break;
                    default:
                        break;
                    }
    
                    myCheck = 0
                }
            }
    
            Connections {
                target: TextDel
                function onFnSendStr(recvStr) {
                    switch (myCheck) {
                    case 1:
                        inpA.text = recvStr
                        break;
                    case 2:
                        inpB.text = recvStr
                        break;
                    default:
                        break;
                    }
                }
            }
    
            Rectangle{
                height: 20
                width: 100
                color: "lightyellow"
    
                TextInput {
                    id: inpA
                    anchors.fill: parent
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            inpA.forceActiveFocus()
                            myCheck = 1
                        }
                    }
                }
    
            }
    
            Rectangle{
                height: 20
                width: 100
                color: "lightblue"
                TextInput {
                    id: inpB
                    anchors.fill: parent
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            inpB.forceActiveFocus()
                            myCheck = 2
                        }
                    }
                }
            }
        }
    
    }
    
    O 1 Reply Last reply 25 Nov 2020, 18:44
    0
    • B Offline
      B Offline
      Bob64
      wrote on 25 Nov 2020, 11:57 last edited by
      #2

      Hi, it's not really clear what you are asking.

      I created a minimal new project and pasted your Item into the empty Window of the project. It displays something but there are errors on the console - e.g. "TextDel is not defined" - and I see more errors if I click the buttons or add text to the TextInputs ("ReferenceError: myCheck is not defined", "Error: Invalid write to global property "myCheck"").

      1 Reply Last reply
      0
      • M morita
        24 Nov 2020, 11:48

        Hello.

        I would like to keep a input state in the "TextInput" after I click a button.

        If I clicked the "TextInput" and clicked the button, the input state was lost.

        I don't know how to solve it.

        Item {
            id: texInp
            anchors.left: parent.left
            anchors.leftMargin: 50
            anchors.verticalCenter: parent.verticalCenter
        
            Row {
                spacing: 10
        
                //テキスト追加
                Button {
                    id:tBtn
                    height: 50
                    width: 50
                    text: "A"
                    onClicked: {
                        switch (myCheck) {
                        case 1:
                            inpA.text = tBtn.text
                            break;
                        case 2:
                            inpB.text = tBtn.text
                            break;
                        default:
                            break;
                        }
                        myCheck = 0
                    }
                }
        
                //一文字削除
                Button {
                    id:aBtn
                    height: 50
                    width: 50
                    text: "Del"
                    onClicked: {
                        switch (myCheck) {
                        case 1:
                            TextDel.fnDelBtn(inpA.text)
                            break;
                        case 2:
                            TextDel.fnDelBtn(inpB.text)
                            break;
                        default:
                            break;
                        }
        
                        myCheck = 0
                    }
                }
        
                Connections {
                    target: TextDel
                    function onFnSendStr(recvStr) {
                        switch (myCheck) {
                        case 1:
                            inpA.text = recvStr
                            break;
                        case 2:
                            inpB.text = recvStr
                            break;
                        default:
                            break;
                        }
                    }
                }
        
                Rectangle{
                    height: 20
                    width: 100
                    color: "lightyellow"
        
                    TextInput {
                        id: inpA
                        anchors.fill: parent
                        MouseArea {
                            anchors.fill: parent
                            onClicked: {
                                inpA.forceActiveFocus()
                                myCheck = 1
                            }
                        }
                    }
        
                }
        
                Rectangle{
                    height: 20
                    width: 100
                    color: "lightblue"
                    TextInput {
                        id: inpB
                        anchors.fill: parent
                        MouseArea {
                            anchors.fill: parent
                            onClicked: {
                                inpB.forceActiveFocus()
                                myCheck = 2
                            }
                        }
                    }
                }
            }
        
        }
        
        O Offline
        O Offline
        ODБOï
        wrote on 25 Nov 2020, 18:44 last edited by
        #3

        hi
        @morita said in How to keep a input state after I click a button:

        I would like to keep a input state in the "TextInput" after I click a button.

        Can this help ? https://doc.qt.io/qt-5/qtquickcontrols2-focus.html
        https://doc.qt.io/qt-5/qml-qtquick-controls2-control.html#focusPolicy-prop

        1 Reply Last reply
        0
        • F Offline
          F Offline
          flowery
          wrote on 28 Nov 2020, 15:17 last edited by
          #4

          @LeLev You have two TextInputs here .The focus will be retained in the last clicked TextInput after you clicked the button also.Please check whether you are getting any error logs.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            morita
            wrote on 4 Dec 2020, 10:31 last edited by
            #5

            Thank you for replying , everyone !

            I soleved this problem by myself.

            The solution is to change a position of the MouseArea.

            I put the MouseArea below the top of the Item.

            I could keep a input state after I clicked a button.

            1 Reply Last reply
            0

            1/5

            24 Nov 2020, 11:48

            • Login

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