PDA

View Full Version : QTextDocument and background-images



pfusterschmied
1st June 2007, 19:26
Hi,
I copy the code from the tutorial:

Tutorial (http://wiki.qtcentre.org/index.php?title=QTextBrowser_with_images_and_CSS&diff=2016&oldid=2014)

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:


#include <QApplication>
#include <QTextDocument>
#include <QTextBrowser>
#include <QVariant>
#include <QDebug>

int main(int argc, char *argv[])
{
// Q_INIT_RESOURCE(application);
QApplication app(argc, argv);

QString html;
html = "<html><head>"
"<link rel='stylesheet' type='text/css' href='format.css'>"
"</head><body>"
"Your HTML code with tags, which have classes or ids. For example "
"<span class='red'>this text is colored red</span>.<br/>"
"And you can also display images: <img src='myImage.jpg'><br/>"
"Combine css and images: <span id='bgimage'>foo bar</span>"
"</body></html>";

// Your CSS code
QString css;
css = "span.red { color:#DE0000; } "
"span#bgimage { background-image: url('myImage.jpg');} ";

// Crate a QTextDocument with the defined HTML, CSS and the images
QTextDocument *doc = new QTextDocument;

/*
* This shows how to bind the images, where the name for QUrl is the same name
* which you use in your HTML and CSS. In QPixmap just use the normal syntax of
* the Qt resource system.
*/
doc->addResource( QTextDocument::ImageResource, QUrl( "myImage.jpg" ), QPixmap( ":myImage" ) );
doc->addResource( QTextDocument::ImageResource, QUrl( "bg.png" ), QPixmap( ":bg" ) );

/*
* And now bind the css, which you have defined in the QString css.
*/
doc->addResource( QTextDocument::StyleSheetResource, QUrl( "format.css" ), css );
doc->setHtml( html ); // binds the HTML to the QTextDocument

/*
* This QTextDocument can now set to any QTextBrowser.
*/
QTextBrowser *tb = new QTextBrowser( 0 );
tb->setDocument( doc );
tb->show();


return app.exec();
}