Results 1 to 14 of 14

Thread: QTextDocument and insertImage

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default QTextDocument and insertImage

    Hey there,

    I'm using a QTextDocument and QTextCursor::insertImage.
    The goal is to remplace : - ) keywords by a in the text document.

    Qt Code:
    1. ZePictureWidget * test = new ZePictureWidget("zeData/zeSmileys/Crying.png");
    2. document()->addResource(QTextDocument::ImageResource, QUrl("test"), test->getPixmap());
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //=============================================================================
    2. //=============================================================================
    3.  
    4. void ZeSmileyController::scanSmiley(QTextDocument & textDocument,
    5. const QString & smiley,
    6. const QString & path)
    7. {
    8. QTextCursor textCursor;
    9. textCursor = textDocument.find(smiley);
    10. while (textCursor.isNull() == false)
    11. {
    12. textCursor.deleteChar();
    13. textCursor.insertImage("test");
    14.  
    15. textCursor = textDocument.find(smiley);
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    Unfortunately when replacing 100 smileys it's very slow, it's like the Cursor is reloading the smileys over and over again.

    Any idea?

  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: QTextDocument and insertImage

    Take a look at QTextDocument::addResource.

  3. #3
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextDocument and insertImage

    1.
    ZePictureWidget * test = new ZePictureWidget("zeData/zeSmileys/Crying.png");
    2.
    document()->addResource(QTextDocument::ImageResource, QUrl("test"), test->getPixmap());

    I've already used that, did I miss something?

  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: QTextDocument and insertImage

    Oh, no... sorry, I read your post too fast and missed the first chunk of code. Did you try profiling the code to find the bottleneck?

  5. #5
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextDocument and insertImage

    Qt Code:
    1. //=============================================================================
    2. //=============================================================================
    3.  
    4. void ZeSmileyController::InsertSmiley(QTextDocument & textDocument)
    5. {
    6. ZeLog::get()->AddLine("ZeSmileyController - insertSmiley\n");
    7.  
    8. QMapIterator<QString, QString> i(mSmileyMap);
    9.  
    10. while (i.hasNext())
    11. {
    12. i.next();
    13. scanSmiley(textDocument, i.key(), i.value());
    14. }
    15. }
    16.  
    17. //=============================================================================
    18. //=============================================================================
    19.  
    20. void ZeSmileyController::scanSmiley(QTextDocument & textDocument,
    21. const QString & smiley,
    22. const QString & path)
    23. {
    24. QTextCursor textCursor;
    25. textCursor = textDocument.find(smiley);
    26. while (textCursor.isNull() == false)
    27. {
    28. textCursor.insertImage(path); // <<< THIS IS SLOW
    29.  
    30. textCursor = textDocument.find(smiley);
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 

    Yes : textCursor.insertImage(path); is the line slowing down everything.

  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: QTextDocument and insertImage

    How about profiling deeper - going into Qt code too?

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTextDocument and insertImage

    Do you replace several smileys at time? Perhaps you should break the QTextDocument-QTextEdit relation meanwhile? I guess QTextEdit ends up doing a lot of unnecessary updates upon every change to the document.
    J-P Nurmi

  8. #8
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextDocument and insertImage

    Yes I'm doing a lot of smiley replacement at a time, since I'm progressively refreshing a text bubble as if somebody was typing on the keyboard.

    how would you break the QTextDocument-QTextEdit relation meanwhile?

    Thanks.
    Last edited by bunjee; 22nd January 2008 at 17:07.

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTextDocument and insertImage

    Whatever it takes to make QTextEdit ignorant of those updates. Block signals, remove and set the document back or modify a copy of the document. Also, make sure you have things like undo-redo-stack disabled.
    J-P Nurmi

  10. The following user says thank you to jpn for this useful post:

    bunjee (22nd January 2008)

  11. #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: QTextDocument and insertImage

    It might be wise to replace the smileys as the user is typing the text - this way you replace only one at the time and only check the current paragraph instead of the whole text that gets bigger and bigger as one types. If you work on a document which already exists, be sure to replace the smileys before you associate the text with a widget as J-P suggests - this way the text will not have to be relaid again and again.

  12. The following user says thank you to wysota for this useful post:

    bunjee (22nd January 2008)

  13. #11
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextDocument and insertImage

    Thanks jpn and wysota,

    This implementation is working way faster :

    Qt Code:
    1. //=============================================================================
    2. //=============================================================================
    3.  
    4. void ZeParagraphWidget::setHtml(const QString & text, bool smiley)
    5. {
    6. //QTextEdit::setHtml(text);
    7.  
    8. QTextDocument * temp = document()->clone();
    9.  
    10. temp->setHtml(text);
    11. if (smiley)
    12. {
    13. ZeSmileyController::get()->InsertSmiley(*temp);
    14. setDocument(temp);
    15. }
    16. emit paragraphUpdate();
    17. }
    To copy to clipboard, switch view to plain text mode 

    I don't seem to have to delete my old document after doing a setDocument, do you confirm on that ?

  14. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTextDocument and insertImage

    Quote Originally Posted by bunjee View Post
    I don't seem to have to delete my old document after doing a setDocument, do you confirm on that ?
    As a practice, you could open src/gui/widgets/qtextedit.cpp and confirm it yourself. It's wonderful to have sources, isn't it?

    PS. src/gui/text/qtextcontrol.cpp
    J-P Nurmi

  15. #13
    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: QTextDocument and insertImage

    A general rule is that a widget or other component will delete the object it doesn't use anymore if it is its child in terms of QObject hierarchy.

  16. #14
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextDocument and insertImage

    And Qt doc confirms this :

    void QTextEdit::setDocument ( QTextDocument * document )
    Makes document the new document of the text editor.
    The parent QObject of the provided document remains the owner of the object. If the current document is a child of the text editor, then it is deleted.
    See also document().
    Last edited by bunjee; 24th January 2008 at 02:23.

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.