Results 1 to 1 of 1

Thread: QTextDocument and background-images

  1. #1
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTextDocument and background-images

    Hi,
    I copy the code from the tutorial:

    Tutorial


    It works's fine, but the background Image doesn't show.
    Why does the <img> picture shown but the <span id='bgimage'>not ????

    Is there an trick?

    Here an short example:
    Qt Code:
    1. #include <QApplication>
    2. #include <QTextDocument>
    3. #include <QTextBrowser>
    4. #include <QVariant>
    5. #include <QDebug>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. // Q_INIT_RESOURCE(application);
    10. QApplication app(argc, argv);
    11.  
    12. QString html;
    13. html = "<html><head>"
    14. "<link rel='stylesheet' type='text/css' href='format.css'>"
    15. "</head><body>"
    16. "Your HTML code with tags, which have classes or ids. For example "
    17. "<span class='red'>this text is colored red</span>.<br/>"
    18. "And you can also display images: <img src='myImage.jpg'><br/>"
    19. "Combine css and images: <span id='bgimage'>foo bar</span>"
    20. "</body></html>";
    21.  
    22. // Your CSS code
    23. QString css;
    24. css = "span.red { color:#DE0000; } "
    25. "span#bgimage { background-image: url('myImage.jpg');} ";
    26.  
    27. // Crate a QTextDocument with the defined HTML, CSS and the images
    28.  
    29. /*
    30.  * This shows how to bind the images, where the name for QUrl is the same name
    31.  * which you use in your HTML and CSS. In QPixmap just use the normal syntax of
    32.  * the Qt resource system.
    33.  */
    34. doc->addResource( QTextDocument::ImageResource, QUrl( "myImage.jpg" ), QPixmap( ":myImage" ) );
    35. doc->addResource( QTextDocument::ImageResource, QUrl( "bg.png" ), QPixmap( ":bg" ) );
    36.  
    37. /*
    38.  * And now bind the css, which you have defined in the QString css.
    39.  */
    40. doc->addResource( QTextDocument::StyleSheetResource, QUrl( "format.css" ), css );
    41. doc->setHtml( html ); // binds the HTML to the QTextDocument
    42.  
    43. /*
    44.  * This QTextDocument can now set to any QTextBrowser.
    45.  */
    46. QTextBrowser *tb = new QTextBrowser( 0 );
    47. tb->setDocument( doc );
    48. tb->show();
    49.  
    50.  
    51. return app.exec();
    52. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by pfusterschmied; 1st June 2007 at 19:46.

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.