Results 1 to 2 of 2

Thread: QPrinter / QPrintDialog issue

  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QPrinter / QPrintDialog issue

    Using Qt 5.2 on Windows 7 64bit. I am building a 32-bit app

    I am trying to get printing working. I have a weird problem that I can't seem to figure out. If I create a QPrinter instance using this code:

    Qt Code:
    1. QPrinter * pPrinter = new QPrinter( QPrinterInfo::defaultPrinter(), QPrinter::ScreenResolution );
    To copy to clipboard, switch view to plain text mode 

    and then look at the values for
    Qt Code:
    1. QPrinter::pageRect( QPrinter::Inch ) and QPrinter::paperRect( QPrinter::Inch )
    To copy to clipboard, switch view to plain text mode 
    I get the results I would expect for my default printer (Letter size paper, 8.5 x 11 inches).

    If I then pass this QPrinter instance to QPrintDialog and open it:

    Qt Code:
    1. if ( pPrinter && pPrinter->isValid() )
    2. {
    3. QPrintDialog dlg( pPrinter );
    4. dlg.setOptions( QAbstractPrintDialog::None );
    5.  
    6. connect( &dlg, SIGNAL( accepted( QPrinter * ) ), this, SLOT( onPrinterAccepted( QPrinter * ) ) );
    7. dlg.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    and then examine those same values in the onPrinterAccepted() slot, those values are now completely messed up, typically QRectF( 0.0, 0.0, -0.0104166, -0.0104166)

    This happens even if I do nothing at all with the dialog - let it display, don't do anything except click the "Print" button.

    If I create the QPrintDialog without passing it a QPrinter instance:

    Qt Code:
    1. dlg.setOptions( QAbstractPrintDialog::None );
    2.  
    3. connect( &dlg, SIGNAL( accepted( QPrinter * ) ), this, SLOT( onPrinterAccepted( QPrinter * ) ) );
    4. dlg.exec();
    To copy to clipboard, switch view to plain text mode 

    I get exactly the same behavior. If I select a different printer from the dialog, it doesn't matter what printer I choose, I still get the same messed up results if I let the dialog use my QPrinter instance or if I look at the one returned to the slot.

    Changing the Unit type in the call to
    Qt Code:
    1. QPrinter::pageRect()
    To copy to clipboard, switch view to plain text mode 
    results in different values being returned, but they are always wrong and always a negative height and width.

    I have confirmed that there is only one plugins/printsupport directory on my machine, so it can't be pulling in a mis-matched "windowsprintersupport" DLL

    I do not know if it is relevant, but the code that does the printing is in a static library, linked into my app. The Print QAction's triggered() signal in the GUI is connected to a slot in the library that kicks off the printing.

    Any ideas what I might be doing wrong?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPrinter / QPrintDialog issue

    Here's a minimal compilable example to demonstrate the problem. I have attached the PNG file to this post. With the QPrintDialog code commented out (as shown below), I get the results in the attached PDF file using PDFCreator as my default printer.

    I would greatly appreciate it if someone could run this code and tell me if it also fails for you when you allow the QPrintDialog code to execute.

    Qt Code:
    1. // PrintingTestDlg.h:
    2.  
    3. #ifndef PRINTINGTESTDLG_H
    4. #define PRINTINGTESTDLG_H
    5.  
    6. #include <QDialog>
    7.  
    8. class QPrinter;
    9. class QLabel;
    10.  
    11. class PrintingTestDlg : public QDialog
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. PrintingTestDlg(QWidget *parent = 0);
    17. ~PrintingTestDlg();
    18.  
    19. protected slots:
    20. void onPrint();
    21. void onPrinterAccepted( QPrinter * pPrinter );
    22.  
    23. private:
    24. QLabel * mpLabel;
    25. };
    26.  
    27. #endif // PRINTINGTESTDLG_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // PrintingTestDlg.cpp:
    2.  
    3. #include "PrintingTestDlg.h"
    4.  
    5. #include <QHBoxLayout>
    6. #include <QPixmap>
    7. #include <QLabel>
    8. #include <QPushButton>
    9. #include <QPrinter>
    10. #include <QPrinterInfo>
    11. #include <QPrintDialog>
    12. #include <QPainter>
    13.  
    14. PrintingTestDlg::PrintingTestDlg(QWidget *parent)
    15. : QDialog(parent)
    16. {
    17. QHBoxLayout * pLayout = new QHBoxLayout( this );
    18.  
    19. QPushButton * pPrintBtn = new QPushButton( this );
    20. pPrintBtn->setText( "Print" );
    21.  
    22. pLayout->addWidget( pPrintBtn );
    23.  
    24. mpLabel = new QLabel( this ) ;
    25. mpLabel->setPixmap( QPixmap( "./logo11w.png" ) );
    26.  
    27. pLayout->addWidget( mpLabel );
    28. setLayout( pLayout );
    29.  
    30. connect( pPrintBtn, SIGNAL( clicked() ), this, SLOT( onPrint() ) );
    31. }
    32.  
    33. PrintingTestDlg::~PrintingTestDlg()
    34. {
    35. }
    36.  
    37. void PrintingTestDlg::onPrint()
    38. {
    39. QPrinterInfo printerInfo = QPrinterInfo::defaultPrinter();
    40.  
    41. QPrinter * pPrinter = 0;
    42. if ( !printerInfo.isNull() )
    43. pPrinter = new QPrinter( printerInfo, QPrinter::ScreenResolution );
    44.  
    45. if ( pPrinter && pPrinter->isValid() )
    46. {
    47. QPrintDialog dlg( pPrinter );
    48. dlg.setOptions( QAbstractPrintDialog::None );
    49.  
    50. // With this line un-commented, the printing works correctly for the
    51. // default printer with default settings
    52. onPrinterAccepted( pPrinter );
    53.  
    54. // Un-comment these two lines, and comment out the line above to see what happens
    55. // when the QPrintDialog is used.
    56. // connect( &dlg, SIGNAL( accepted( QPrinter * ) ), this, SLOT( onPrinterAccepted( QPrinter * ) ) );
    57. // dlg.exec();
    58.  
    59. delete pPrinter;
    60. }
    61. }
    62.  
    63. void PrintingTestDlg::onPrinterAccepted( QPrinter * pPrinter )
    64. {
    65. QString printName = pPrinter->printerName();
    66. QRectF paperRect = pPrinter->paperRect( QPrinter::DevicePixel );
    67. QRectF pageRect = pPrinter->pageRect( QPrinter::DevicePixel );
    68. QRect plotRect = mpLabel->rect();
    69.  
    70. double xscale = pageRect.width() / double( plotRect.width() );
    71. double yscale = pageRect.height() / double( plotRect.height() );
    72. double scale = qMin( xscale, yscale );
    73.  
    74. QPainter painter( pPrinter );
    75. painter.translate( paperRect.x() + pageRect.width() / 2,
    76. paperRect.y() + pageRect.height() / 2 );
    77. painter.scale( scale, scale );
    78. painter.translate( -plotRect.width() / 2, -plotRect.height() / 2 );
    79.  
    80. mpLabel->render( &painter );
    81. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Attached Files Attached Files

Similar Threads

  1. Replies: 1
    Last Post: 14th May 2013, 07:54
  2. QPrintDialog pre initialisation?
    By conti in forum Newbie
    Replies: 0
    Last Post: 25th May 2012, 09:20
  3. Translate QPrintDialog
    By vcp in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2009, 19:57
  4. How to use QPrinter without QPrintDialog?
    By pascal456 in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2006, 19:57
  5. QPrinter::PrinterMode and QPrinter::setResolution??
    By SkripT in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2006, 11:59

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.