How to write in a text Label made in QML

  • Hey,

    I am new with QML and i have same problems.
    I am trying to write from a file in same labels made with QML.
    This is how looks my labels:

    Page1 {
                Label {
                    text: qsTr("Same text")
                    anchors.centerIn: parent

    Basically what i want to do is to create more labels and to populate them with same data from a .txt file.
    Samething like this :

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    int main(int argc, char *argv[])
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
      //open the .txt or .xml file and write data to labels
        return app.exec();

    For example when i use "normal interface" i do something like this:


    I am trying to find a way to do the same thing with QML.
    Thank you :)

  • Qt Champions 2017

    You will not be able do like this(off course we can do with some deep dive. But not for you). Label is QML object. You need to modify this in QML context only. When do you want do set the text like this ? It should be based on some some event/signal/mouseclick etc. You need to write as signalhandler in QML do this.

    I suggest you to go through MouseArea & Signal Handlers in QML. You should be able do this.

  • It is not possible to get a handle to such an object from c++ code?

  • Qt Champions 2017

    As I said it is possible but not recommended. It is not straightforward. You can achieve same thing in qml itself.

  • Can you use some view where Label is the delegate and the text items are in a model? (

    Opening a file isn't so straightforward in qml. If you can use a view/model and readonly xml file, you can use XmlListModel. If not, it may be easier to use c++ for reading the file. But then a c++ model and qml view is better than manipulating the qml objects from c++. In this case it should be pretty simple because in you code you read the file (create the model) only once. Just create a QStringList, set it as context property (see the qml documentation examples for that) and use it as a model in your qml view.

Log in to reply