Results 1 to 7 of 7

Thread: Accesing Images in a html doc stored as QResource file

  1. #1
    Join Date
    Oct 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Post Accesing Images in a html doc stored as QResource file

    Hi all,
    I have a .qrc file in which i have stored a html file and few images in the same folder. The html file uses these images to display them. I'am setting this html document in a QTextbrowser. I am loading the html file using the sethtml() function. But i'm unable to load the images in the document. How do i get the images to be displayed? Here is my code:

    QFile htmlString(":/pathtohtml");
    QImage preview(":/img.png");

    if(htmlString.open( QIODevice::ReadOnly))
    {
    textbrowser->setHtml(htmlString.readAll());
    }

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,593
    Thanks
    13
    Thanked 1,587 Times in 1,516 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Accesing Images in a html doc stored as QResource file

    Write the HTML to use relative paths:
    Qt Code:
    1. <html>
    2. <body>
    3. <img src="test.png"/> <!-- relative path >
    4. </body>
    5. </html>
    To copy to clipboard, switch view to plain text mode 
    put the HTML and image in the same place in the resources:
    Qt Code:
    1. <!DOCTYPE RCC><RCC version="1.0">
    2. <qresource>
    3. <file>./test.html</file>
    4. <file>./test.png</file>
    5. </qresource>
    6. </RCC>
    To copy to clipboard, switch view to plain text mode 
    then load the HTML using QTextBrowser::setSource():
    Qt Code:
    1. #include <QApplication>
    2. #include <QTextBrowser>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QApplication app(argc, argv);
    7. tb.setSource(QUrl(QLatin1String("qrc:/test.html")));
    8. tb.show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 23rd October 2014 at 09:55.

  3. The following user says thank you to ChrisW67 for this useful post:

    sRB (23rd October 2014)

  4. #3
    Join Date
    Oct 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Post Re: Accesing Images in a html doc stored as QResource file

    Hi,
    Thanks alot .
    I'm using this html file as the first item to be loaded in a Qlist . The second item in the list is setting another html document which is created dynamically during runtime (without being stored as Qt resources data) using the sethtml() function. The hyperlinks within the second document don't seem to be working, when I click on any of the links there it brings me back to the first html document. I don't understand why this should happen. How do i clear the setsource() of the first document to make the textbrowser consider the current one?
    Last edited by sRB; 23rd October 2014 at 13:55.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,593
    Thanks
    13
    Thanked 1,587 Times in 1,516 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Accesing Images in a html doc stored as QResource file

    I am not sure exactly what your problem is.

    You load the new content into the QTextBrowser by calling setSource() or setHtml() with the new content. The old content is replaced.

    If the generated HTML must link to resources that are in the resource file then you should generate URIs using the qrc: scheme to identify those resources. So, calling setHtml() with this text should also work:
    Qt Code:
    1. <html>
    2. <body>
    3. <img src="qrc:/test.png"/> <!-- URI uses qrc: scheme -->
    4. </body>
    5. </html>
    To copy to clipboard, switch view to plain text mode 

    You cannot trivially hard code a link into the QRC based file that will cause the loading of a arbitrary bunch of HTML from a variable.
    Last edited by ChrisW67; 23rd October 2014 at 21:16.

  6. #5
    Join Date
    Oct 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Accesing Images in a html doc stored as QResource file

    Hi,
    My Problem is that, I have a List that has two elements,
    1) The first item in the list is, I have a html file which is stored as resource file and I'm accessing that file using textbrowser->setSource(QUrl(QLatin1String("qrc:/resources/test.html")));
    2) The second item is, In my application I write code which creates html tags as strings and finally a large single string containing the whole document is passed as an argument to sethtml(resultString).
    Both are displayed properly within the TextBrowser

    Now, When I click on any of the hyperlinks within the html document created using setHtml() , the links don't seem to work.
    So I was wondering why this behaviour should happen?

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,593
    Thanks
    13
    Thanked 1,587 Times in 1,516 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Accesing Images in a html doc stored as QResource file

    Links to what? What do the URLs look like? What does "don't seem to work" actually mean?

  8. #7
    Join Date
    Oct 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Accesing Images in a html doc stored as QResource file

    Links are the hyperlinks in a html document. They refer to other sections within the same document. These are created using anchor tags <a>

Similar Threads

  1. Replies: 1
    Last Post: 23rd October 2014, 10:00
  2. Replies: 2
    Last Post: 27th April 2011, 07:43
  3. Why the images from the HTML file is not loaded??
    By rleojoseph in forum Qt Programming
    Replies: 8
    Last Post: 2nd March 2011, 11:29
  4. qresource file alias problem with svg
    By giotto in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2008, 15:59
  5. getting images out of html
    By hijinks in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2006, 23:17

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.