Results 1 to 2 of 2

Thread: How to display images in QTextBrowser ?

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to display images in QTextBrowser ?

    I want to display images in my QTextBRowser. Those images are from different websites.

    So far, I am able to connect to those sites with a QHttp and so on...

    But the pages are displayed, and every time an image should appear, I see a symbol, but not the images.

    This is what the debug prints:

    QTextBrowser: Cannot open 'http://localhost/website/images/logo.png' for reading
    QFSFileEngine:pen: No file name specified

    Why?

  2. #2
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display images in QTextBrowser ?

    Quote Originally Posted by probine View Post
    But the pages are displayed, and every time an image should appear, I see a symbol, but not the images.

    This is what the debug prints:

    QTextBrowser: Cannot open 'http://localhost/website/images/logo.png' for reading
    QFSFileEngine:pen: No file name specified

    Why?
    I would guess that you try to give QTextBrowser some html such as "<img src="XXX" />", right?

    The problem in that case is that, QTextBrowser is no WebBrowser and does not know how to handle http.

    One possible aproach that I can see (but have not tested) is to create a new class inherrited from QTextBrowser and overload the loadResource function in a way where you check for the names you are interested in and resolve them.
    For example like this (again not tested)
    Qt Code:
    1. QVariant MyBrowser::loadResource(int type, const QUrl &name)
    2. {
    3. if (type == QTextDocument::ImageResource
    4. && name.scheme() == QLatin1String("mypics")) {
    5. QImage correctImage;
    6. //lookup the correct QImage from a cache
    7. return QVariant::fromValue(correctImage);
    8. } else {
    9. return QTextBrowser::loadResource(type, name);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    Then you should be able to show images when you call
    Qt Code:
    1. myTextBrowser.append(QLatin1String("<img src=\"mypics://theCurrentPicture.png\" />"));
    To copy to clipboard, switch view to plain text mode 


    Disclaimer: Shot from the Hip, not tested...might be completely the wrong track
    Last edited by camel; 12th January 2007 at 09:39. Reason: make the conversion to QVariant explicit

Similar Threads

  1. QTextBrowser display logo ?
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2007, 15:53
  2. Loading images in QTextBrowser
    By nisha0609 in forum Qt Programming
    Replies: 1
    Last Post: 3rd January 2007, 09:44
  3. Widget to display images and text ?
    By probine in forum Qt Tools
    Replies: 4
    Last Post: 9th October 2006, 20:49
  4. [QT4] QtextBrowser and image size (win)
    By sebgui in forum Qt Programming
    Replies: 0
    Last Post: 28th March 2006, 21:01
  5. how to adjust sizes of QTextBrowser?
    By Pan Wojtas in forum Qt Programming
    Replies: 2
    Last Post: 7th February 2006, 22:25

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.