Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved] Loading a file larger than 5.242.880 bytes insert newline in QPlainTextEdit



  • Below is a scheme I use to load a large file. But when the file exceeds 5,242,880 bytes, an extra new line is inserted. So character 5.242.881 is inserted on the next line.

    I've created a text file with 32767 lines of exactly 160 bytes (including 2 bytes for \r\n, thus 158 characters + \r\n).
    Line 32768 has exactly 80 characters. This file load fine.

    When I add 1 character to line 32768, this character will be displayed on a new line. Obviously not correct.

    Is this a bug in my code or somewhere else?

    @void MainWindow::openFile(QString filePath)
    {
    ui->plainTextEdit->clear();
    file->setFileName(filePath);
    if (file->open(QFile::ReadOnly))
    QTimer::singleShot(10, this, SLOT(readFilePart()));

    startRead.start();
    ui->plainTextEdit->viewport()->setCursor(Qt::WaitCursor);
    ui->plainTextEdit->setUpdatesEnabled(false);
    }

    void MainWindow::readFilePart()
    {
    qint64 bytesRead = file->read(buffer, bufsize - 1);
    buffer[bytesRead] = 0;
    ui->plainTextEdit->appendPlainText(buffer);

    qDebug() << "duration:" << startRead.elapsed() << ", bytesRead:" << bytesRead;

    if (bytesRead == bufsize - 1)
    QTimer::singleShot(10, this, SLOT(readFilePart()));
    else
    {
    qDebug() << "rest: {" << buffer << "}";
    QTimer::singleShot(10, this, SLOT(closeFile()));
    }
    }

    void MainWindow::closeFile()
    {
    file->close();
    ui->plainTextEdit->setUpdatesEnabled(true);
    ui->plainTextEdit->viewport()->unsetCursor();
    }
    @

    project files are at:
    "MainWindow.ui":http://pastebin.com/2AYnPe1L
    "MainWindow.h":http://pastebin.com/x327G261
    "MainWindow.cpp":http://pastebin.com/8bx1havh


  • Moderators

    So if you think there is a bug: "File a bug report":https://bugreports.qt-project.org/ . Talking about it here will unfortunately not get it fixed:/



  • Before filing a bug report I want to exclude making a bug myself :)

    [edit]Which I obviously failed to do ;-)[/edit]




  • Moderators

    Well, the best person to find out whether it is a bug or not is the developer that wrote the code. If you file a bug report that person will notice the issue. If you post it to the forum the likelihood of that happening is way lower. And if you send attach a small project that demonstrates, then the time it takes to reproduce the issue should not be too big. It is pretty easy to reject a bug as invalid. It is much harder finding bugs that people talk about somewhere on the internet:)

    Before somebody suggests maintainers or whoever can just copy the issues from here into the bug tracker: That fails as soon as there is a question back to the user that had the problem. So that is not a solution.



  • This line
    @ui->plainTextEdit->appendPlainText(buffer);@

    inserts a newline after reading 65535 bytes in my example.

    Issue solved. See bugreport.


Log in to reply