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:


The relevant code of the application is the following:

Qt Code:
  1. void MainWindow::openFile(const QString &path)
  2. {
  3. QString fileName = path;
  4.  
  5. if (fileName.isNull())
  6. fileName = QFileDialog::getOpenFileName(this,
  7. tr("Open File"), "", "C++ Files (*.cpp *.h)");
  8.  
  9. if (!fileName.isEmpty()) {
  10. QFile file(fileName);
  11. if (file.open(QFile::ReadOnly | QFile::Text))
  12. editor->setPlainText(file.readAll());
  13.  
  14. QVector<quint32> test;
  15. test.append(8); // I want the eighth line to be highlighted
  16. editor->highlightLines(test);
  17. }
  18. }
To copy to clipboard, switch view to plain text mode 
and

Qt Code:
  1. #include "texteditwidget.h"
  2.  
  3. TextEditWidget::TextEditWidget(QWidget *parent) :
  4. QTextEdit(parent)
  5. {
  6. setAcceptRichText(false);
  7. setLineWrapMode(QTextEdit::NoWrap);
  8.  
  9. }
  10.  
  11.  
  12.  
  13. // Called to highlight lines of code
  14. void TextEditWidget::highlightLines(QVector<quint32> linesNumbers)
  15. {
  16.  
  17. // Highlight just the first element
  18. this->setFocus();
  19. QTextCursor cursor = this->textCursor();
  20. cursor.setPosition(0);
  21. cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, linesNumbers[0]);
  22. this->setTextCursor(cursor);
  23. QTextBlock block = document()->findBlockByNumber(linesNumbers[0]);
  24. QTextBlockFormat blkfmt = block.blockFormat();
  25. // Select it
  26. blkfmt.setBackground(Qt::yellow);
  27. this->textCursor().mergeBlockFormat(blkfmt);
  28. }
To copy to clipboard, switch view to plain text mode 
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