Results 1 to 9 of 9

Thread: How to print textDocument on virtual printer?

  1. #1
    Join Date
    Jan 2008
    Posts
    7
    Thanks
    2

    Default How to print textDocument on virtual printer?

    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:

    Qt Code:
    1. #include <QApplication>
    2. #include <QPainter>
    3. #include <QPrintDialog>
    4. #include <QPrinter>
    5. #include <QTextDocument>
    6. #include <QTimer>
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11.  
    12. QPrinter printer;
    13. QPainter painter(&printer);
    14.  
    15. //QPrintDialog *dialog = new QPrintDialog(&printer);
    16. //dialog->setWindowTitle("Print Document");
    17. //if (dialog->exec() != QDialog::Accepted)
    18. // return 0;
    19.  
    20. QTextDocument textDocument;
    21. textDocument.setPlainText("hello");
    22. textDocument.print(&printer);
    23.  
    24. QTimer::singleShot(0, &app, SLOT(quit()));
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by wysota; 17th January 2008 at 12:28. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2008
    Posts
    7
    Thanks
    2

    Default Re: How to print textDocument on virtual printer?

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to print textDocument on virtual printer?

    what happens if you comment the timer?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Jan 2008
    Posts
    7
    Thanks
    2

    Default Re: How to print textDocument on virtual printer?

    It didn't help, application as before prints a blank page.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to print textDocument on virtual printer?

    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.
    Last edited by high_flyer; 17th January 2008 at 13:11.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Jan 2008
    Posts
    7
    Thanks
    2

    Default Re: How to print textDocument on virtual printer?

    Sorry, but I didn't understand what I need to do =)

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to print textDocument on virtual printer?

    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
    Qt Code:
    1. textDocument.show();
    To copy to clipboard, switch view to plain text mode 
    before
    Qt Code:
    1. textDocument.print(..);
    To copy to clipboard, switch view to plain text mode 

    and I think you should comment out
    Qt Code:
    1. QPainter painter(&printer);
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. The following user says thank you to high_flyer for this useful post:

    mSergey (17th January 2008)

  9. #8
    Join Date
    Jan 2008
    Posts
    7
    Thanks
    2

    Default Re: How to print textDocument on virtual printer?

    Wow!

    Deleting of the

    Qt Code:
    1. QPainter painter(&printer);
    To copy to clipboard, switch view to plain text mode 

    helped!

    high_flyer, thanks a lot for your help!
    Last edited by mSergey; 17th January 2008 at 13:57.

  10. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to print textDocument on virtual printer?

    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!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.