Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to keep a input state after I click a button



  • 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
                        }
                    }
                }
            }
        }
    
    }


  • 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"").





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



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


Log in to reply