如何检测何时在TextEdit组件中获得焦点

时间:2018-09-18 18:55:06

标签: qt qml qt5

我已经在文档中进行了搜索,以寻找到TextEdit组件被聚焦时可以挂接到的信号,以便可以进行一些格式化,但是却什么也找不到。我有一个TextEdit组件,该组件允许用户编辑数字,但在不集中显示时会使用区域设置显示数字,例如:

TextEdit {
    text: object.number.toLocaleString()
    onFocus: { text = object.number; }
    onEditingFinished: {
        // Validation
        object.number = parseInt(text);
    }
}

有什么我想念的吗?

1 个答案:

答案 0 :(得分:1)

Is this what you want to do? I used onPropertyChanged to achieve it by creating 2 text edit fields so I can change focus from one to the other. What you will see here is the text change when you click back and forth between TextEdits

import QtQuick 2.7
import QtQuick.Window 2.0

Window
{
    id: mainWindow
    width: 800
    height: 800
    visible: true

    TextEdit {
        id: t1
        text: "defaut"    
        onFocusChanged: {
            if(focus)
                text = "focused"
            else
                text = "not focused"
        }
    }

    TextEdit {
        anchors.top: t1.bottom
        text: "default2"    
        onFocusChanged: {
            if(focus)
                text = "focused"
            else
                text = "not focused"
        }
    }
}