• 2805阅读
  • 1回复

[提问]qml 如何让输入框失去焦点? [复制链接]

上一主题 下一主题
离线wang6354
 

只看楼主 倒序阅读 楼主  发表于: 2021-04-09

我的界面上只有三个控件: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

只看该作者 1楼 发表于: 2022-04-09
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"
        }
    }
}
Best Regards
快速回复
限100 字节
 
上一个 下一个