Results 1 to 13 of 13

Thread: QTextEdit optimization

  1. #1
    Join Date
    May 2007
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QTextEdit optimization

    Hi,

    I wanted to optimize a QTextEdit control by highlighting only the visible part of it.

    I got the start/end cursor positions of the QTextEdit's viewport. But what can I do after to call the viewport's painter to redraw only the visible part ?

    Any help will be appreciated, thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit optimization

    It already does that - redraws only the visible part.

  3. #3
    Join Date
    May 2007
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit optimization

    Hi wysota,

    thanks for your answer, but I found it rather slow when I have an highlighter full of keywords and regexp based state rules. Is that a normal behaviour ?
    I also noticed that loading/pasting a long file takes a long time for QTextEdit.
    I'm working with Python, but I don't think the problem lies here.

    Cheers.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit optimization

    Depends on the complexity and structure of your highlighter. Normally it should be called for a single paragraph at a time, so it also depends on the structure of your document. I suggest you use a profiler to find the bottleneck of your application.

  5. #5
    Join Date
    May 2007
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit optimization

    Thanks for clarifying this point wysota : is it called by paragraph (group of lines separated by a blank one or more) or by line ?

    Edit : it seems it is called by paragraph, right ?
    Last edited by kib2; 5th November 2007 at 11:01.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit optimization

    Yes, one paragraph at a time.

  7. #7
    Join Date
    May 2007
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit optimization

    In a Qt Quarterly http://doc.trolltech.com/qq/qq21-syntaxhighlighter.html article it is said that :

    QSyntaxHighlighter makes this possible through its "state" mechanism. When we finish highlighting a line, we can associate a state with the line (e.g., "Inside C-Style Comment"), which we can retrieve when we start highlighting the following line. The state is stored as an int.
    The article is talking about lines, not paragraphs.

    So, let's suppose I wanted to highlight comments like this :

    ### a comment
    , maybe on several lines ###

    Here 'text' is the QString argument of my highlightBlock method. I put inside some print statements for debugging purpose.

    When I call it, having typed : "### a comment", highlightBlock's text returns "### a comment".

    After pressing enter and typing the second line : ", maybe on several lines ###", highlightBlock's text returns :", maybe on several lines ###".

    So it confuses me : its not a paragraph but the second line only. I expected :
    "### a comment\n, maybe on several lines ###".

    I must miss something here, but don't know what exactly.

  8. #8
    Join Date
    Jan 2006
    Posts
    369
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit optimization

    In QTextDocument (when you load from *.txt files) a paragraph is a line. Maybe that is what's confusing for you.

    Now I have a question:
    Does anyone know how to query the syntax highlighter for it's state? I want to know the state at a random offset of some random paragraph.

  9. #9
    Join Date
    May 2007
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit optimization

    Thanks elcuco,
    for the state, you can use QSyntaxHighlighter::currentBlockState.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit optimization

    Quote Originally Posted by elcuco View Post
    Does anyone know how to query the syntax highlighter for it's state? I want to know the state at a random offset of some random paragraph.
    I don't think you can do that. The state is probably saved within the text document itself by using custom user data (probably using QTextBlockUserData). You can check the source code, maybe the data is accessible there, but I wouldn't bet on it. To me the highlighter is only called when appropriate and has no control (nor knowledge) about what happens when it's not called.

  11. #11
    Join Date
    Jan 2006
    Posts
    369
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: QTextEdit optimization

    Ok, I started looking at the code:

    Qt Code:
    1. int QSyntaxHighlighter::previousBlockState() const
    2. {
    3. Q_D(const QSyntaxHighlighter);
    4. if (!d->currentBlock.isValid())
    5. return -1;
    6.  
    7. const QTextBlock previous = d->currentBlock.previous();
    8. if (!previous.isValid())
    9. return -1;
    10.  
    11. return previous.userState();
    12. }
    To copy to clipboard, switch view to plain text mode 

    and d->currenltBlock is defined as:
    Qt Code:
    1. class QSyntaxHighlighterPrivate : public QObjectPrivate
    2. {
    3. Q_DECLARE_PUBLIC(QSyntaxHighlighter)
    4. ...
    5. QTextBlock currentBlock;
    6. ...
    7. };
    To copy to clipboard, switch view to plain text mode 

    And I do see that QTextBlock::userState() is public, which means you can know the state of the beginning of a paragraph. Much simpler then I thought of ... even tough it's "not good enough for me".

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit optimization

    I'd say it's a bit stupid, because the docs say that setUserState() saves an integer that can be for example used for syntax highlighting. This suggests it can be used for something else effectively breaking the highlighter.

    I just read your question again - you wanted the state of a position inside a paragraph. I don't think there is such concept. The state is defined for the position between two paragraphs. I think that instead you can query for the char format of a specified position. It probably won't do you much good either, but maybe you can work something out with it. What is the purpose of such interest?

  13. #13
    Join Date
    Jan 2006
    Posts
    369
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit optimization

    Yes, it seems a bug in the Qt4 API, it should be documented that this is 'too fragile" to mess with.

    I am toying with the idea of using the syntax highlighter code to know the state of each word in a paragraph. This will help me (for example) knowing which words can I send to a spell checker (only words inside comments or strings for example).

    Yes, the variable I showed before only gives me the state in the beginning (or end) or each paragraph.

Similar Threads

  1. QTextEdit, sizeHint, QWidget
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2007, 08:25
  2. Re-implement mouse events of QTextEdit
    By Ankitha Varsha in forum Qt Programming
    Replies: 2
    Last Post: 14th October 2006, 16:55
  3. Optimization specifics with qmake
    By bitChanger in forum Qt Programming
    Replies: 1
    Last Post: 29th August 2006, 17:55
  4. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03
  5. Painting to QTextEdit
    By gesslar in forum Qt Programming
    Replies: 8
    Last Post: 18th February 2006, 18:40

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.