Important: Please read the Qt Code of Conduct -

Print two separate pages

  • I have two pages that I want to print
    Now I can print them separately by calling the printer and the painter twice,
    But I want to call the printer once, and print them
    How can I do it?

    QPixmap pix =ui->stackedWidget->widget(0)->grab();
    QPrinter printer(QPrinter::HighResolution);
          QPainter painter;
          double xscale = printer.pageRect().width() / double(pix.width());
          double yscale = printer.pageRect().height() / double(pix.height());
          double scale = qMin(xscale, yscale);
          painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
                            printer.paperRect().y() + printer.pageRect().height() / 2);
          painter.scale(scale, scale);
          painter.translate(-width() / 2, -height() / 2); 
          painter.drawPixmap(0, 0, pix);
    QPrinter printer;
           QPrintDialog dlg(&printer, 0);
           if (dlg.exec() == QDialog::Accepted) {
               // Output chart as QImage
               /// do some thing
               // Print the image
               QPainter painter(&printer);

  • Lifetime Qt Champion


    Is QPrinter::newPage what you are looking for ?

  • @sgaist It's work tnx

                 painter.drawPixmap(0, 0, pix);

  • @sgaist I have another problem
    the page has scrollBar
    When I grab It

    QPixmap pix = ui-> stackedWidget-> widget (0) -> grab ();

    , Prints half of the page
    How do I print the entire page??

  • Lifetime Qt Champion

    What kind of widget is that ?

  • a page? do you mean a widget?

  • @sgaist
    I want to print a page of the stack widget that has the scroll bar
    This page contains line Edit, Push Button
    It's solve with :

    QPixmap pix = ui->scrollAreaWidgetContents->grab();

Log in to reply