PDA

View Full Version : Qt QTextEdit loading just half text file



enthalio1
31st July 2012, 17:10
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:
http://i.stack.imgur.com/BvgZr.jpg

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

mvuori
31st July 2012, 18:33
It seems that the edit control needs some peace and quiet after loading the file to set everything up...

I figured that that might be so and inserted QApplication::processEvents(); after setPlainText(). Now, the test file loaded almost... When I duplicated QApplication::processEvents();, the whole file loaded. Of course, this is not nice at all, but at least there is a workaround that will let you proceed with things.

enthalio1
31st July 2012, 19:00
Thank you very much, however I think I should fill a bug report.. a signal to give advice that the text loading has completed would be nice

d_stranz
1st August 2012, 03:54
I was thinking about mvuori said - adding calls to processEvents(): split up your openFile() method into more steps, so that Qt has time to handle events. So maybe you could try something like this:

- openFile() just opens the file and reads the contents into a QString.
- QTextEdit::setPlainText( const QString &) is a slot, so have your MainWindow emit a signal: textLoaded( const QString & ) after the file is read, and connect this to the QTextEdit slot.
- make your highlightLines() a slot also, and emit a signal from MainWindow to connect to it.

If you do this, then instead of doing lots of processing with no event handling, you let Qt do the signals and slots thing it was designed to do well. You can always call a slot as if it was an ordinary function (which it is), but when you use the signal / slot mechanism instead, Qt can do its magic.


Thank you very much, however I think I should fill a bug report.

Probably not a good idea, because the Qt folks would probably tell you to redesign your code to do things the Qt way instead of the C++ way.