PDA

View Full Version : how to print an html file?



patcito
7th January 2006, 04:03
I have a "QFile myhtmlFile", is there a way to print that easy with
Qt4? I know there is QPrinter but I couldn't find where to set the
file I want to print, anyone?

thanx in advance

Patcito

MarkoSan
7th January 2006, 09:44
Use QPrinter.

wysota
8th January 2006, 19:17
QPrinter is a paint device. You need to layout that html file using Qt richtext capabilities (QDocumentLayout probably) and paint that layout "on" the printer (like you were using a QPainter).

Woffca
31st October 2006, 10:25
To print a content of a HTML file, you would need to use a QTextDocument. I would use something like the following to do it:

HTH :)



void print()
{
QFile file("myHTML.html");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

QString htmlContent;
QTextStream in(&file);
in >> htmlContent;

QTextDocument *document = new QTextDocument();
document->setHtml(htmlContent);

QPrinter printer;

QPrintDialog *dialog = new QPrintDialog(&printer, this);
if (dialog->exec() != QDialog::Accepted)
return;

document->print(&printer);

delete document;
}

rmagro
31st August 2008, 15:09
and what to do if the file I have to print is a binary file (for instance pdf file, .doc file )??
Thank you for your help
Roby

wysota
31st August 2008, 17:50
You have to have an application that understands these types of files.