PDA

View Full Version : How to print textDocument on virtual printer?



mSergey
17th January 2008, 12:26
Unfortunately I don't have physical printer on my machine at the moment, so I use PDFCreator virtual printer (http://www.pdfforge.org/products/pdfcreator). This makes it easy to test my printing application, but there are some problems unhappy

Order Form application from the set of standard Qt examples prints all its documents normal, but my application fails for some reason.

Here is source code of my application:


#include <QApplication>
#include <QPainter>
#include <QPrintDialog>
#include <QPrinter>
#include <QTextDocument>
#include <QTimer>

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

QPrinter printer;
QPainter painter(&printer);

//QPrintDialog *dialog = new QPrintDialog(&printer);
//dialog->setWindowTitle("Print Document");
//if (dialog->exec() != QDialog::Accepted)
// return 0;

QTextDocument textDocument;
textDocument.setPlainText("hello");
textDocument.print(&printer);

QTimer::singleShot(0, &app, SLOT(quit()));
return app.exec();
}

If I compile application with comments PDFCreate prints a blank page (without "hello" text). If I show QPrintDialog for setting by uncommenting four lines then nothing happens (even printing a blank page doesn't occur).

What is wrong? Thanks! :)

Qt 4.3.3
Windows XP SP2

mSergey
17th January 2008, 12:39
With QPrintDialog blank page will be printed if I click on "Cancel". It's very strange, in consideration of that Rich Text -> Order Form example works fine :confused:

high_flyer
17th January 2008, 12:53
what happens if you comment the timer?

mSergey
17th January 2008, 13:02
It didn't help, application as before prints a blank page.

high_flyer
17th January 2008, 13:05
Maybe its more along these lines:

Certain widgets, such as QTextEdit and QGraphicsView, display rich content that is typically managed by instances of other classes, such as QTextDocument and QGraphicsScene. As a result, it is these content handling classes that usually provide printing functionality, either via a function that can be used to perform the complete task, or via a function that accepts an existing QPainter object. Some widgets provide convenience functions to expose underlying printing features, avoiding the need to obtain the content handler just to call a single function.

And I still think using QPainter out side a paint event might also to do with it.

mSergey
17th January 2008, 13:21
Sorry, but I didn't understand what I need to do =)

high_flyer
17th January 2008, 13:27
I just thought QTextDocument might have some internal way of printing, as the text above suggest, but now that I have read through it, I see that what you did is ok in that regard.
Try adding


textDocument.show();

before


textDocument.print(..);


and I think you should comment out

QPainter painter(&printer);

mSergey
17th January 2008, 13:42
Wow!

Deleting of the


QPainter painter(&printer);

helped!

high_flyer, thanks a lot for your help! :D :)

high_flyer
17th January 2008, 13:49
that's because the text document has its own painter, and you were overriding it, and it uses its own painter on the printer object you give it.
As I said, QPainter is not to be found out side of a paintEvent()!
Glad it helped! :)