PDA

View Full Version : editor->setHtml doesn't



GreyGeek
15th February 2007, 21:16
For all last year I used the following code to generate a printout of a report. I loaded a QString var, strHTMLtext, with html markup in order to create formatting. Then I'd create an editor object and use its "setHtml" function to parse the markup code into formatted text.
Then I'd create a document object using the editor object, and send that to the printer.

I't stopped working sometime between September of last year, the end of last year's Homestead season, and this week, the beginning of the new season. The only significant change is the upgrading to 4.2.2.

Here's the code.


QFont f("TypeWriter",10);
QTextEdit *editor = new QTextEdit(this);
editor->setHtml(strHTMLtext);
editor->setCurrentFont(f);
QTextDocument *document = editor->document();
QPrinter printer;
QPrintDialog *dlg = new QPrintDialog(&printer,this);
if (dlg->exec() == QDialog::Accepted){
document->print(&printer);
}
delete dlg;
delete editor;
If I make the editor visible in a window and then send it to the printer I get an good printout, otherwise I get a single, blank page.

As a temporary fix I've created a temp.exe which is an editor app that reads the html file from the HD, and uses setHtml(in.readALL()) to parse it into the editor. Then I print it. It's not the one button click it used to be, but it works.

jacek
15th February 2007, 21:21
You don't need QTextEdit at all.

Try this:

QFont f( "TypeWriter", 10 );

QTextDocument document;
document.setDefaultFont( f );
document.setHtml( strHTMLtext );

QPrinter printer;
QPrintDialog dlg( &printer, this );
if( dlg.exec() == QDialog::Accepted ) {
document.print( &printer );
}

GreyGeek
17th February 2007, 03:37
You don't need QTextEdit at all.

Try this:

QFont f( "TypeWriter", 10 );

QTextDocument document;
document.setDefaultFont( f );
document.setHtml( strHTMLtext );

QPrinter printer;
QPrintDialog dlg( &printer, this );
if( dlg.exec() == QDialog::Accepted ) {
document.print( &printer );
}

Thanks, Jacek, I'll try that.

BTW, I found out why this code stopped working. Somewhere between 4.1 and 4.2.2 the QTextEdit object now has to display it contents on the screen before it will send them to the printer.

GreyGeek
17th February 2007, 04:38
You don't need QTextEdit at all.

Try this:

QFont f( "TypeWriter", 10 );

QTextDocument document;
document.setDefaultFont( f );
document.setHtml( strHTMLtext );

QPrinter printer;
QPrintDialog dlg( &printer, this );
if( dlg.exec() == QDialog::Accepted ) {
document.print( &printer );
}

Well, I tried this method and it produced the same results. :( It spits a blank page out of the printer. I've submitted this problem to Trolltech support. I'll let you know what they tell me.

jacek
17th February 2007, 14:15
Well, I tried this method and it produced the same results. :( It spits a blank page out of the printer.
Hmmm... It works for me. Maybe there's something wrong with strHTMLtext? Also try printing without changing the default font.