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. QTextStream read issue
Forum Updated to NodeBB v4.3 + New Features

QTextStream read issue

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 2 Posters 2.8k Views 2 Watching
  • 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.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #1

    Hi! I want to read some text from file and display in QPlainTextEdit. File is about 2 MB. I have created QObject class and connect signals and slots with thread. The problem is it still freezes window even when the thread is running.

    I have decided to use QtConcurrent.

    My code:

    QFile dbFile;
    QTextStream dbTextStream;
    dbFile.setFileName(":Test/Test.txt");
    dbFile.open(QIODevice::ReadOnly);
    dbTextStream.setDevice(&dbFile);
    dbTextStream.setCodec("windows-1251");
    QFuture<QString> myData = QtConcurrent::run(this, &TestWindow::loadTestData);
    ui->plainTextEdit->appendPlainText(myData.result());
    
    QString TestWindow::loadTestData()
    {
      QString data = dbTextStream.readAll();
      return data;
    }
    

    No errors, no warnings but it blocks window (GUI) for seconds.

    So I have to use QFutureWatcher:

    QFutureWatcher<QString> watcher;
     connect(&watcher, QFutureWatcher::finished, this, &TestWindow::handleFinished);
    
    QFuture<QString> myData = QtConcurrent::run(this, &TestWindow::loadDatabase);
    watcher.setFuture(myData);
    
    void DatabaseManager::handleFinished()
    {
        // Append text data to QPlainTextEdit
    }
    

    When I compile it throws errors: C2955: 'QFutureWatcher': use of class template requires template argument list 'QFutureWatcherBase::finished': non-standard syntax; use '&' to create a pointer to member

    So how to fix it?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      I'd avoid emitting 2MB of text at once.

      Note that your while loop will only run once since you call readAll in it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Cobra91151C 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        I'd avoid emitting 2MB of text at once.

        Note that your while loop will only run once since you call readAll in it.

        Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by Cobra91151
        #3

        @SGaist

        I have used readLine but it displays only one line?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Because you call emit testData outside of the loop thus only sending the last line read.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Cobra91151C 1 Reply Last reply
          0
          • SGaistS SGaist

            Because you call emit testData outside of the loop thus only sending the last line read.

            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by
            #5

            @SGaist

            I changed code to:

            while (!dbTextStream.atEnd()) {
                    data = dbTextStream.readLine();
                    emit databaseData(data);
                }
            

            But window is still not responding and after a while displays only one line?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Are you calling setText rather than append ?

              Be aware that with such a loop you are basically spamming the event loop that will try to re-render the text widget content for each new line you read. You should rather use batches and give the event loop some time to work.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              Cobra91151C 1 Reply Last reply
              0
              • SGaistS SGaist

                Are you calling setText rather than append ?

                Be aware that with such a loop you are basically spamming the event loop that will try to re-render the text widget content for each new line you read. You should rather use batches and give the event loop some time to work.

                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by
                #7

                @SGaist

                Yes. The problem was that I use setText rather than append and now it sets all text, but still window not responding. What do you mean about using batches?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  I don't know how many lines your text file contains but basically you are likely requesting tens of thousands of line to be added in a fraction of a second.

                  Doing batches means doing things by chunks i.e. load a certain amount of data, send it further and start again until everything has been read..

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Cobra91151C 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    I don't know how many lines your text file contains but basically you are likely requesting tens of thousands of line to be added in a fraction of a second.

                    Doing batches means doing things by chunks i.e. load a certain amount of data, send it further and start again until everything has been read..

                    Cobra91151C Offline
                    Cobra91151C Offline
                    Cobra91151
                    wrote on last edited by
                    #9

                    @SGaist

                    Yes. The file contains a lot of lines.
                    But why QThread can't handle it? I have the same program written in C# and it opens and displays one file without dividing it by chunks. Are there other classes in Qt to read huge text files?

                    1 Reply Last reply
                    0
                    • Cobra91151C Offline
                      Cobra91151C Offline
                      Cobra91151
                      wrote on last edited by
                      #10

                      The main post was updated to display current issue.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        As the error mention, you forgot to put the & and the template type. i.e. &QFutureWatcher<int>::finished

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1

                        • Login

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