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 (file.open(QIODevice::ReadWrite)) {
                QTextStream stream(&file);
                stream << ba << endl
            }
            file.close();
    }
    

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

    QFile file(m_source);
    QString fileContent;
    if ( file.open(QIODevice::ReadOnly) ) {
        QString line;
        QTextStream t( &file );
        do {
            line = t.readLine();
            fileContent += line;
        }
        while (!line.isNull());
        qDebug() << "SOURCE" << line;
        file.close();
        file.remove();
    }
    

    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: {
        console.log(myFile.read())
        textarea1.text = myFile.read();
    }
    

    "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
        model: myFile.read()
        delegate: Text { text: modelData }
    }
    


  • This post is deleted!

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.