0

I have a problem: my project is a very simple one with a QTextEdit and a QSyntaxHighlighter, I'm trying to load a .cpp file and highlighting just the eighth line of that file, but the QTextEdit can't load the entire file if I ask it to highlight the line.

The following image shows the problem:

enter image description here

The relevant code of the application is the following:

void MainWindow::openFile(const QString &path)
{
    QString fileName = path;

    if (fileName.isNull())
        fileName = QFileDialog::getOpenFileName(this,
            tr("Open File"), "", "C++ Files (*.cpp *.h)");

    if (!fileName.isEmpty()) {
        QFile file(fileName);
        if (file.open(QFile::ReadOnly | QFile::Text))
            editor->setPlainText(file.readAll());

        QVector<quint32> test;
        test.append(8); // I want the eighth line to be highlighted
        editor->highlightLines(test);
    }
}

and

#include "texteditwidget.h"

TextEditWidget::TextEditWidget(QWidget *parent) :
    QTextEdit(parent)
{
    setAcceptRichText(false);
    setLineWrapMode(QTextEdit::NoWrap);

}



// Called to highlight lines of code
void TextEditWidget::highlightLines(QVector<quint32> linesNumbers)
{

    // Highlight just the first element
    this->setFocus();
    QTextCursor cursor = this->textCursor();
    cursor.setPosition(0);
    cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, linesNumbers[0]);
    this->setTextCursor(cursor);
    QTextBlock block = document()->findBlockByNumber(linesNumbers[0]);
    QTextBlockFormat blkfmt = block.blockFormat();
    // Select it
    blkfmt.setBackground(Qt::yellow);
    this->textCursor().mergeBlockFormat(blkfmt);
}

However if you want to test the project with the cpp file I used (in the directory FileToOpen\diagramwidget.cpp), here's the complete source

http://idsg01.altervista.org/QTextEditProblem.zip

I've been trying to solve this for a lot of time and I'm starting to wonder if this isn't a bug or something similar

Johnny Pauling
  • 12,701
  • 18
  • 65
  • 108

2 Answers2

1

The QTextEdit can't accept such a big amount of text at one piece. Split it, for example like this:

if (!fileName.isEmpty()) {
    QFile file(fileName);
    if (file.open(QFile::ReadOnly | QFile::Text))
    {
        QByteArray a = file.readAll();

        QString s = a.mid(0, 3000);//note that I split the array into pieces of 3000 symbols.
        //you will need to split the whole text like this. 
        QString s1 = a.mid(3000,3000);

        editor->setPlainText(s);
        editor->append(s1);
    }
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • but if I comment the "highlighting" line out, the QTextEdit can handle it all without problems – Johnny Pauling Jul 31 '12 at 16:52
  • 1
    @JohnnyPauling, it's hard to say why this happens... but you can just split the data to blocks in a cycle, and it should be ok always. – SingerOfTheFall Jul 31 '12 at 16:54
  • Look at my post, I solved it thanks to the Qt forum, anyway I'm marking your answer as useful because you wasted your time trying to help me. Thank you! – Johnny Pauling Jul 31 '12 at 17:09
0

It seems that the QTextEdit control needs time after each loading, setting a QApplication:processEvents(); after setPlainText() solves the problem although it's not an elegant solution.

Bart
  • 19,692
  • 7
  • 68
  • 77
Johnny Pauling
  • 12,701
  • 18
  • 65
  • 108