Results 1 to 2 of 2

Thread: QPrinter problems

  1. #1
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    64
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QPrinter problems

    Hello,

    It seems that it is almost impossible to get consistant printing results with
    QPrinter class on Linux and Windows and with different kinds of printers.

    Is QPrinter just not ready for professional use or am I doing something wrong?

    I created a QPainter widget and draw some curves on the screen. Then I want
    to print this to a printer, a postscriptfile and a pdf-file.

    Here's a part of my code:
    Qt Code:
    1. void ViewCurve::print_to_printer()
    2. {
    3. QPrinter printer(QPrinter::ScreenResolution);
    4.  
    5. printer.setOutputFormat(QPrinter::NativeFormat);
    6. printer.setOrientation(QPrinter::Landscape);
    7. printer.setFullPage(FALSE);
    8. printer.setPageSize(QPrinter::A4);
    9. printer.setCreator("Printertest");
    10.  
    11. // printf("resolution is %i\n", printer.resolution());
    12. // printf("width is %i\n", printer.pageRect().width());
    13. // printf("height is %i\n", printer.pageRect().height());
    14.  
    15. // printer.setResolution((int)((double)width() / 10.6));
    16.  
    17. #ifndef WIN32
    18. // printer.setPrintProgram("lpr");
    19. #endif
    20.  
    21. QPrintDialog printerdialog(&printer, this);
    22. printerdialog.setWindowTitle("Print");
    23.  
    24. if(printerdialog.exec()==QDialog::Accepted)
    25. {
    26. QPainter paint(&printer);
    27.  
    28. drawCurve(&paint);
    29. }
    30. }
    31.  
    32.  
    33. void ViewCurve::print_to_postscript()
    34. {
    35. // double width_factor;
    36.  
    37. QFileDialog fchooser;
    38.  
    39. QStringList fileNames;
    40.  
    41. fchooser.setViewMode(QFileDialog::Detail);
    42. fchooser.setConfirmOverwrite(1);
    43. fchooser.setFileMode(QFileDialog::AnyFile);
    44. fchooser.setAcceptMode(QFileDialog::AcceptSave);
    45. fchooser.setWindowTitle("Print to PostScript");
    46. fchooser.setLabelText(QFileDialog::FileName, "File");
    47. fchooser.setDefaultSuffix("ps");
    48. #ifndef WIN32
    49. fchooser.setDirectory(getenv("HOME"));
    50. #endif
    51. fchooser.setFilter("PostScript files (*.ps *.PS)");
    52. fchooser.selectFile("printertest.ps");
    53.  
    54. if(!(fchooser.exec() == QDialog::Accepted))
    55. {
    56. return;
    57. }
    58.  
    59. fileNames = fchooser.selectedFiles();
    60.  
    61. QPrinter printer(QPrinter::ScreenResolution);
    62.  
    63. printer.setOutputFormat(QPrinter::PostScriptFormat);
    64. printer.setPageSize(QPrinter::A4);
    65. printer.setOrientation(QPrinter::Landscape);
    66. printer.setOutputFileName(fileNames.at(0));
    67.  
    68. // printer.setResolution((int)((double)width() / 10.6));
    69.  
    70.  
    71. // printf("resolution is %i\n", printer.resolution());
    72. // printf("width is %i\n", printer.pageRect().width());
    73. // printf("height is %i\n", printer.pageRect().height());
    74.  
    75. // width_factor = ((double)printer.pageRect().width()) / ((double)width());
    76.  
    77. QPainter paint(&printer);
    78.  
    79. // drawCurve(&paint, printer.pageRect().width(), printer.pageRect().height(), width_factor);
    80. drawCurve(&paint);
    81. }
    82.  
    83.  
    84. void ViewCurve::print_to_pdf()
    85. {
    86. QFileDialog fchooser;
    87.  
    88. QStringList fileNames;
    89.  
    90. fchooser.setViewMode(QFileDialog::Detail);
    91. fchooser.setConfirmOverwrite(1);
    92. fchooser.setFileMode(QFileDialog::AnyFile);
    93. fchooser.setAcceptMode(QFileDialog::AcceptSave);
    94. fchooser.setWindowTitle("Print to PDF");
    95. fchooser.setLabelText(QFileDialog::FileName, "File");
    96. fchooser.setDefaultSuffix("pdf");
    97. #ifndef WIN32
    98. fchooser.setDirectory(getenv("HOME"));
    99. #endif
    100. fchooser.setFilter("PDF files (*.pdf *.PDF)");
    101. fchooser.selectFile("printertest.pdf");
    102.  
    103. if(!(fchooser.exec() == QDialog::Accepted))
    104. {
    105. return;
    106. }
    107.  
    108. fileNames = fchooser.selectedFiles();
    109.  
    110. QPrinter printer(QPrinter::ScreenResolution);
    111.  
    112. printer.setOutputFormat(QPrinter::PdfFormat);
    113. printer.setOutputFileName(fileNames.at(0));
    114. printer.setPageSize(QPrinter::A4);
    115. printer.setOrientation(QPrinter::Landscape);
    116. // printer.setResolution((int)((double)width() / 10.6));
    117.  
    118. QPainter paint(&printer);
    119.  
    120. drawCurve(&paint);
    121. }
    To copy to clipboard, switch view to plain text mode 
    If I choose QPrinter::HighResolution instead of QPrinter::ScreenResolution,
    I need to set the resolution manually because the size of the widget is
    dependant of the screen, for example 1280 x 1024 or 1024 x 768. This
    depends on the user ofcourse. Even then, I get different sizes on different
    printers. One printer prints OK, on another one there is a part missing.

    For reasons, I can not repaint the widget on a (bigger) fixed scale.

    According to the documentation of Qt 4.3.2, using QPrinter::ScreenResolution
    should do the job but in reality, it prints wrong sizes.

    I attached a complete Qt testproject including the .pro file.

    Are there any people with the same experience. Is there any solution?

    I'm using OpenSUSE 10.2 and Windows XP.

    Thanks in advance,

    Teuniz
    Attached Files Attached Files

  2. #2
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    64
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPrinter problems

    I found the solution. I choose HighResolution for the printer. Then I ask for the printable area and then I scale the widget on which I will paint so that it fits
    into the printable area. Like this:
    Qt Code:
    1. QPrinter printer(QPrinter::HighResolution);
    2.  
    3. printer.setOutputFormat(QPrinter::NativeFormat);
    4. printer.setPageSize(QPrinter::A4);
    5. printer.setOrientation(QPrinter::Landscape);
    6.  
    7. #ifdef Q_WS_X11
    8. printer.setPrintProgram("lpr");
    9. #endif
    10.  
    11. width_factor = ((double)printer.pageRect().width()) / ((double)width());
    12. height_factor = ((double)printer.pageRect().height()) / ((double)height());
    13.  
    14. QPainter paint(&printer);
    15.  
    16. paint.scale(width_factor, height_factor);
    17.  
    18. drawCurve(&paint);
    To copy to clipboard, switch view to plain text mode 
    Strange that nobody here could point me to this...

    Regards,

    Teuniz

Similar Threads

  1. QPrinter on QGraphicsScene Border Problem
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2007, 15:49
  2. Problems with QPrinter with custom pageSize
    By davit in forum Qt Programming
    Replies: 2
    Last Post: 25th June 2007, 04:23
  3. How to use QPrinter without QPrintDialog?
    By pascal456 in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2006, 19:57
  4. QPrinter on Solaris
    By ToddAtWSU in forum Qt Programming
    Replies: 4
    Last Post: 24th October 2006, 20:29
  5. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 15:39

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.