Results 1 to 4 of 4

Thread: Qt QTextEdit loading just half text file

  1. #1
    Join Date
    Jul 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Qt QTextEdit loading just half text file

    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

  2. #2
    Join Date
    May 2011
    Posts
    240
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Qt QTextEdit loading just half text file

    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:rocessEvents(); after setPlainText(). Now, the test file loaded almost... When I duplicated QApplication:rocessEvents();, 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.

  3. #3
    Join Date
    Jul 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt QTextEdit loading just half text file

    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

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,314
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Qt QTextEdit loading just half text file

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 3
    Last Post: 2nd August 2012, 14:46
  2. QTextEdit loading takes long time
    By sreedhar in forum Qt Programming
    Replies: 12
    Last Post: 21st March 2011, 11:29
  3. Replies: 1
    Last Post: 3rd September 2008, 15:16
  4. QTextEdit + paste rich text as plain text only
    By Yong in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 17:45
  5. QTextEdit and delayed image loading
    By Vladimir in forum Qt Programming
    Replies: 5
    Last Post: 27th April 2007, 09:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.