Seite 1 von 1

QTextDocument und CSS

Verfasst: 31. Mai 2007 11:23
von pfusterschmied
Hi Leute,

Ich möchte Daten aus einer DB in einer Tabelle ausdrucken. Die Tabelle erstellen und der Ausdruck funktionieren bereits wunderbar. Nun möchte ich, der Tabelle ein Hintergrundbild zuweisen. Leider wird der dazu passende HTML Tag "background" von QT nicht unterstützt. Nun dachte ich an eine Zuweisung via CSS. Leider reicht mein Wissen für dieses Vorhaben nicht aus. Kann mir jemand weiterhelfen, bzw. hat einen anderen Ansatz. Danke!!

Code: Alles auswählen

while (myQuery.next())
{
		myQuery.next();
		QString lastName = myQuery.value(9).toString();

		// HTML Template für Druck
		myReport += "<table border=\"2\" bgcolor=\"#eeeeee\"><tr><td>"+ lastName+"</td><td>"+lastName+"</td><td></td></tr><tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr></table>";

		myPrinter.newPage();
	
}

QString style;
style +="TABLE {background-image: url(:/images/liste1.png)}";
	
myTextDocument.setDefaultStyleSheet(style);
myTextDocument.setHtml(myReport);
myTextDocument.print(&myPrinter);

Beantworte selbst

Verfasst: 1. Juni 2007 17:41
von pfusterschmied
Beantworte mir nach langem Suchen die Frage selbst. Vielleicht hilfts ja jemanden: http://wiki.qtcentre.org/index.php?titl ... oldid=2014.


Zentrales Problem bleibt jedoch! Kein Hintergrundbild bei Tabellen oder ähnlichen. Kann mir jemand sagen warum ein <img> im QTextBrowser angezeigt wird jedoch keine zugewiesenen Hintergrundbilder????

Ist dies ein Bug oder muss man speziell etwas beachten?
Laut o.g. Tutorial sollte es funktioniert .... :cry:
Vielleicht findet sich ja jemand für eine schnelle Antwort...

Warum!

Verfasst: 1. Juni 2007 19:43
von pfusterschmied
Bitte helft mir weiter....
Hier ein Bsp. nach dem o.g. Tutorial.

Warum wird das <img> angezeigt und der <span id='bgimage'> nicht ????

:x

Code: Alles auswählen

#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();
}

Verfasst: 1. Juni 2007 20:21
von upsala
1. Wenn hier keiner Antwortet, heißt das nicht, daß keiner will, sondern daß keiner kann.
2. Ist bg.png überhaupt vorhanden, und im gleichen Pfad wie das ander Image?

Background-Image

Verfasst: 1. Juni 2007 20:36
von pfusterschmied
Zuerst danke für deine Antwort.
Wenn du den Code genauer ansiehst, lade ich 2 mal das gleiche Bild.
Einmal als <img src='myImage.jpg'> und einmal per CSS (span#bgimage { background-image: url('myImage.jpg'))

Sorry, das zweite Bild ist verwirrent.

Mit dem IMG Tag wird das Bild angezeigt. mit dem CSS Tag nicht. Es liegt auch nicht am CSS da jede andere Formatierung übernommen wird nur der background-image wird verworfen. Auch eine Hintergrundzuweisung mittels HTML funktioniert auch nicht.

Könnte dies ein Bug sein oder hab ich irgendwas total übersehen?

Verfasst: 4. Juni 2007 17:52
von Zandru
background images sind (noch?) nicht supported in QTextBrowser

Schade

Verfasst: 4. Juni 2007 18:10
von pfusterschmied
Zandru hat geschrieben:background images sind (noch?) nicht supported in QTextBrowser
Danke für dein Antwort ich, hab jetzt einen kleinen Workaround gemacht, indem ich kein bg-image gesetzt habe sonden einfach verschachtelte Tabellen verwende mit <img>.... Auch keine optimale Lösung...

Verfasst: 4. Juni 2007 18:33
von Zandru
QTextBrowser ist halt noch kein vollständiger Firefox-Ersatz ;-) aber kannst das ja mal an qt-bugs@trolltech.com schreiben, bisher ist nix zu dem Thema im Tasktracker.

Verfasst: 4. Juni 2007 18:35
von pfusterschmied
Werde ich machen, sobald ich mein anderes problem gelöst habe ...