Important: Please read the Qt Code of Conduct -

(SOLVED) Print problem with QGraphicsScene without using QPrintDialog!

  • I have an strange problem with printing a QGraphicsScene!

    If i comment printDialog.exec() line, i just have Qt text on output without the whole things i have inside my scene on my <HP printer> but if i output the result to <Microsoft XPS Document Writer>, it works fine and i have correct output on xps file.

    If i uncomment it , a dialog appears (what i don't want!) and even if i cancel it or close it, i still have correct output plus "Qt Here" text!

    any idea about printDialog.exec() ?! what does it do to printer even when i close it or cancel it ?

    QPrinter printer(QPrinter::HighResolution);

        printer.setPrinterName("HP LaserJet 1010");
        QRectF pageRect(23,16, 748,1088);// = printer.pageRect();
        QGraphicsScene m_scene_print(pageRect);
        .... some code ....
        QPrintDialog printDialog( &printer,this );
        QPainter painter( &printer &#41;;
        painter.drawText(rect(), Qt::AlignCenter, "Qt Here");
        m_scene_print.render( &painter);   //, printer.pageRect(), m_scene_print.sceneRect(), Qt::KeepAspectRatio );


  • Why you continue to print even if you reject it ? Basically the code should like
    if (printDialog.exec() == QDialog::Accepted) {
    QPainter painter( &printer );
    painter.drawText(rect(), Qt::AlignCenter, "Qt Here");
    ... ETC

  • the problem is that i don't want that the user see the printDialog! but without using printDialog.exec() i don't have correct output but if use it even by pressing cancle button or closing dialog, it works correct!

  • It is possible to print without using a QPrintDialog, by directly calling QPrinter member functions to set things up. To cite from docs:
    "The most important parameters are:

    • setOrientation() tells QPrinter which page orientation to use.
    • setPaperSize() tells QPrinter what paper size to expect from the printer.
    • setResolution() tells QPrinter what resolution you wish the printer to provide, in dots per inch (DPI).
    • setFullPage() tells QPrinter whether you wantto deal with the full page or just with the part the printer can draw on.
    • setCopyCount() tells QPrinter how many copies of the document it should print. "

    Also you can set many other QPrinter properties.
    Also see "QPrinterInfo": and their two static public members.

  • Hm! it seems there was a problem with my scene ! seems loading thing inside my scene take times and if i show any dialog (even aboutQt messageBox ....) and press ok or cancel or exit , loads complete and works fine! strange! ;)

Log in to reply