Results 1 to 5 of 5

Thread: QTextEdit::setHtml(): how can I pass a string with an embedded image?

  1. #1
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTextEdit::setHtml(): how can I pass a string with an embedded image?

    Hello,
    is it possibile to pass to setHtml() a string with a self-contained image? I know I can do the following:

    Qt Code:
    1. QString html;
    2. html.append("<img src=\"c:\\temp\\sample.png\">");
    3. myTextEdit->setHtml(html);
    To copy to clipboard, switch view to plain text mode 

    and it works. However, I would like to get rid of the external image file (I am generating it at runtime).

    My first attempt was to build the html incrementally by repeteadly calling QTextEdit::insertHtml() and QTextEdit::insertImage(), but that messes things up. I guess that's because I am generating the html by recursion (visiting a tree), so the "start" and "end" tags are usually separated by many calls to "insertHtml()", and formatting gets lost in the way.

    I also tried to encode the images in the html using some "base64" encoding snippet found on the net, but it doesn't work.

    Thanks

    Qt 4.5.1, Vista Sp1, VS Express 2008

  2. The following user says thank you to mattc for this useful post:

    NicholasSmith (25th August 2009)

  3. #2
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit::setHtml(): how can I pass a string with an embedded image?

    Ooops, I must be stupid: the base64 approach works fine. I probably messed up something on my first attempt.

  4. #3
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit::setHtml(): how can I pass a string with an embedded image?

    No, it doesn't work. It was just a placeholder image displayed by QTextEdit, not the actual image. The question is still open.

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTextEdit::setHtml(): how can I pass a string with an embedded image?

    Years ago I write an article at the wiki. This my be a startpoint for you to integrate images into your QTextEdit. http://wiki.qtcentre.org/index.php?title=QTextBrowser_with_images_and_CSS

    P.s.: Don't know if it still works with 4.5...

  6. The following user says thank you to Lykurg for this useful post:

    mattc (21st June 2009)

  7. #5
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit::setHtml(): how can I pass a string with an embedded image?

    Thank you Lykurg,
    it works pretty well! Here is the working code for future reference:

    Qt Code:
    1. QString html;
    2.  
    3. QImage img("c:\\temp\\sample.png"); // todo: generate image in memory
    4. myTextEdit->document()->addResource(QTextDocument::ImageResource, QUrl("sample.png" ), img);
    5.  
    6. html.append("<p><img src=\":sample.png\"></p>");
    7.  
    8. myTextEdit->setHtml(html);
    To copy to clipboard, switch view to plain text mode 

  8. The following 2 users say thank you to mattc for this useful post:

    ehxor (28th October 2012), sivambigai (15th November 2010)

Similar Threads

  1. ActiveQt: How to pass image to ActiveX object?
    By AndreasSchlempp in forum Qt Programming
    Replies: 2
    Last Post: 16th February 2009, 13:44
  2. QTableWidgetItems, String + Image
    By shooogun in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2008, 05:53
  3. Create pixmap image from string
    By Morea in forum Qt Programming
    Replies: 5
    Last Post: 17th November 2006, 17:38

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.