Using variable as text in Text element immediately

2019-08-14 08:14发布

问题:

If I have the following code:

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.

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?

回答1:

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.



标签: qt qml qt5