Important: Please read the Qt Code of Conduct -

QML - How display a text file on ListView?

  • QML - How display a text file on ListView?

    I want to do a telnet client. There is no problem to connect modem and read QTcpSocket.

    void iDirectClient::when_socket_ready_read() {
        QByteArray ba = m_socket->readAll();
        qDebug() << "\r\nRead:\r\n" << ba;
        QString filename = "my_file.txt";
            QFile file(filename);
            if ( {
                QTextStream stream(&file);
                stream << ba << endl

    and I use FileIO for read a text file in QML.Here is my code:

    QFile file(m_source);
    QString fileContent;
    if ( ) {
        QString line;
        QTextStream t( &file );
        do {
            line = t.readLine();
            fileContent += line;
        while (!line.isNull());
        qDebug() << "SOURCE" << line;

    When I create my_file.txt, there is no problem. It seems like

    "677 = T12V_KU_SAT
    623 = SBC2_KU_SEA
    615 = IS19_KU_SWP" (these are beam list.)

    but I read this text file in QML, with this code:

    FileIO {
        id: myFile
        source: "my_file.txt"
        onError: console.log(msg)
        Component.onCompleted: {
    Component.onCompleted: {
        textarea1.text =;

    "677 = T12V_KU_SAT623 = SBC2_KU_SEA615 = IS19_KU_SWP" output is like this.

    I want to display every beam in listview because I need to beams names are clickable.There is a problem with line feed.Could you help me about another problem that is displaying this text on ListView in QML. I can use QStringList but I don't know how can I show QStringList(from c++) to ListView in QML. Maybe you can give an example for this.

    I hope I explained clear my problem.Thank you!

  • I can't say that I understood your code, but if you'll change the return value of your reading method (read(), I presume) to QStringList, then you can display it with ListView in QML like this:

    ListView {
        //Layout.preferredHeight: childrenRect.height
        delegate: Text { text: modelData }

  • This post is deleted!