Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to write in a text Label made in QML
Forum Update on Monday, May 27th 2025

How to write in a text Label made in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bogdan112
    wrote on last edited by Bogdan112
    #1

    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[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    
      //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:

    ui->label->setText("SameStringHere");
    

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

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      4
      • B Offline
        B Offline
        Bogdan112
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

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

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          4
          • E Offline
            E Offline
            Eeli K
            wrote on last edited by
            #5

            Can you use some view where Label is the delegate and the text items are in a model? (http://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html)

            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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved