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. How can I load big text file into QPlainTextEdit?
Forum Updated to NodeBB v4.3 + New Features

How can I load big text file into QPlainTextEdit?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 5.9k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello, I wonder why I can't load 10MB of content into QPlainTextEdit.
    Windows default editor, Notepad able to load 10MB of text file. but QPlainTextEdit unable to do it.
    The code is something like this.

    QFile file(fileName);
    file.open(QFile::ReadOnly);
    QString content = QString::fromUtf8(file.readAll());
    ui->plainTextEdit->setPlainText(content);
    file.close();
    

    It stuck at setPlainText.
    It never finish.
    Plus, Windows task manger says the application uses 400MB of memory + 25% of cpu for loading 10MB of text file.
    How can I load big text file into QPlainTextEdit?

    I also tried something like below, but no luck

    ui->plainTextEdit->setReadOnly(true);
    ui->plainTextEdit->setUndoRedoEnabled(false);
    setUpdatesEnabled(false);
    
    1 Reply Last reply
    0
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      Hi,
      first of all please try file.open(QIODevice::Text | QIODevice::ReadOnly);

      Secondly, readAll(); does not perform any error checking. Should you still experience issues please try to loop the read with content.append(file.readLine()); and place some error checking inside the loop.

      Btw. I just generated 10MB txt file of random characters - no issues reading that one with the code you provided.

      From what your task mngr shows - you have some endless loop there.

      For more information please re-read.

      Kind Regards,
      Artur

      ? 1 Reply Last reply
      2
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Thank you very much for your reply.
        I modified code so that it looks like this:

        QFile file(fileName);
        file.open(QIODevice::Text | QIODevice::ReadOnly);
        QString content;
        while(!file.atEnd())
            content.append(file.readLine());
        ui->plainTextEdit->setPlainText(content);
        file.close();
        

        content.append works very well.
        But ui->plainTextEdit->setPlainText(content);
        never finish.
        Can I have some more hint?

        1 Reply Last reply
        0
        • artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          I am sorry to hear you still have problems.
          Other workaround that comes to my mind is to load the text into QTextDocument, set it to plain text and attach into text editor.

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          0
          • artwawA artwaw

            Hi,
            first of all please try file.open(QIODevice::Text | QIODevice::ReadOnly);

            Secondly, readAll(); does not perform any error checking. Should you still experience issues please try to loop the read with content.append(file.readLine()); and place some error checking inside the loop.

            Btw. I just generated 10MB txt file of random characters - no issues reading that one with the code you provided.

            From what your task mngr shows - you have some endless loop there.

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @artwaw I just found that if I there exist newline characters, it loads well. But if there is no newline character, it never finish. I wonder if this is normal behavior.

            1 Reply Last reply
            0
            • artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #6

              Great to hear you've solved it.
              I do not know if it is standard behavior however I always use QTextDocument together with editors.

              For more information please re-read.

              Kind Regards,
              Artur

              ? 1 Reply Last reply
              0
              • artwawA artwaw

                Great to hear you've solved it.
                I do not know if it is standard behavior however I always use QTextDocument together with editors.

                ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                @artwaw I also noticed that if I disable wordwrap with

                ui->plainTextEdit->setWordWrapMode(QTextOption::NoWrap);
                

                it loads 10MB text file without newline well.
                I think the problem was based on Qt's word wrap.

                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