查看完整版本: [-- qml 如何让输入框失去焦点? --]

QTCN开发网 -> Qt QML开发 -> qml 如何让输入框失去焦点? [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

wang6354 2021-04-09 22:51

qml 如何让输入框失去焦点?


我的界面上只有三个控件:1.输入框,2.Button,3. 单选按钮
我发现当输入框获得焦点后,光标在输入框里闪烁,此时当我单击button或者单选按钮后,光标依然在输入框闪烁,

我的问题
1. 这是否说明当单击Button或者单选按钮后焦点还在输入框里?
2. 如何实现当单击Button或者单选按钮后让输入框失去焦点?

我的代码如下:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls 1.4
Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Rectangle{
        width: 240
        height: 80
        border.color:"#FFFF00"
        border.width: 3
        anchors.centerIn: parent
        radius: 5
        TextInput{
            id:input
            anchors.fill:parent
            anchors.margins: 2
        }
    }

    Row{
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        height: 30
        spacing: 10
        ExclusiveGroup{
            id:group
        }
        RadioButton{
            id:radio_1
            exclusiveGroup: group
            text: qsTr("Base")
        }
        RadioButton{
            id:radio_2
            exclusiveGroup: group
            text: qsTr("Advanced")
        }
        RadioButton{
            id:radio_3
            exclusiveGroup: group
            text: qsTr("Advanced")
        }
    }

    Button{
        id:btn
        text: "OK"

        onClicked: {
            console.log(input.text)
            console.log(btn.text)
        }
    }
}

meteorliu 2022-04-09 11:37
1) 单机别的后焦点会失去
2) 表示按你说的,文本输入框,单选和按钮并没有出现你的情况,代码如下可参考
Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Column{
        x: 10
        y: 10
        Rectangle{
            width: 150
            height: 50
            border.color: "black"
            TextInput{
                width: 150
                height: 50
            }
        }

        RadioButton{
            width: 150
            height: 50
            text: "a"
        }
        RadioButton{
            width: 150
            height: 50
            text: "b"
        }
        RadioButton{
            width: 150
            height: 50
            text: "c"
        }
        Button{
            width: 150
            height: 50
            text: "Click Me"
        }
    }
}


查看完整版本: [-- qml 如何让输入框失去焦点? --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled