Search code examples
c++qtfile-ioqmltelnet

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.

I hope I explained clear my problem.Thank you!


Solution

  • The string returned by QTextStream::readLine() does not contain end-of-line characters according to the documentation.