Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QML - How display a text file on ListView?
QtWS25 Last Chance

QML - How display a text file on ListView?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qmlqstringlistqt quicktext
3 Posts 2 Posters 3.3k 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.
  • J Offline
    J Offline
    justlooking
    wrote on 4 Aug 2016, 11:59 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • V Offline
      V Offline
      veryqtperson
      wrote on 4 Aug 2016, 12:15 last edited by veryqtperson 8 Apr 2016, 13:33
      #2

      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 }
      }
      
      J 1 Reply Last reply 4 Aug 2016, 12:44
      0
      • V veryqtperson
        4 Aug 2016, 12:15

        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 }
        }
        
        J Offline
        J Offline
        justlooking
        wrote on 4 Aug 2016, 12:44 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0

        1/3

        4 Aug 2016, 11:59

        • Login

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