在 Text 元素中使用变量作为文本会立即激活 onTextChanged [英] Using variable as text in Text element immediately activates onTextChanged

查看:33
本文介绍了在 Text 元素中使用变量作为文本会立即激活 onTextChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下代码:

import QtQuick 2.10
import QtQuick.Window 2.10

Window {
    id: app
    visible: true
    width: 640
    height: 480
    property bool txt: false

    Text {
        text: app.txt
        onTextChanged: { console.debug("Text changed") }
    }
}

我在应用程序加载后立即在控制台中显示文本已更改",但是如果我手动将文本设置为类似

I get "Text changed" displayed in my console as soon as the app loads, however if I set the text manually to something like

    Text {
        text: "Some text"
        onTextChanged: { console.debug("Text changed") }
    }

我的控制台中不会显示文本已更改",除非我确实有一些在应用加载后更改文本的内容.

I don’t get the "Text changed" display in my console unless I actually have something that changes the text after the app loads.

这是正常行为吗?有没有办法将变量用作文本,但不会在应用加载后立即激活 onTextChanged?

Is this normal behaviour? Is there a way to use the variable as the text but not have onTextChanged activate as soon as the app loads?

推荐答案

是的,这是 qml 的正常行为,因为您的第一个 text 属性 是 "" (nothing),当你给它一个 variety 时,它会从 "" 变成 app.txt.如果你手动设置文本,在这种情况下,没有什么可以改变的.

Yes, it's normal behaviour for qml, becuase your first text property is "" (nothing), and when you give it a variety it changes from "" to app.txt. If you set manually text, in this case, there is nothing to change.

这篇关于在 Text 元素中使用变量作为文本会立即激活 onTextChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆