print preview widget

  • like anyone has an example with how to use print preview widget?
    i'm having a main window that has a widget where in paint event i draw some shape, this widget where i draw being in a layout, and i wanna make something like when i trigger and paint action to get the preview like an raport.
    i tried something like this, idk i should make another widget? idk understand how they work
    void MainWindow::on_actionPrint_triggered()
    QPrinter *printer = new QPrinter(QPrinter::PrinterResolution);
    QPrintPreviewWidget *preview = new QPrintPreviewWidget(printer,ui->drawGraphicView);

  • Qt Champions 2017

    @rest said in print preview widget:


    Sample here

    key point is paintRequested
    connect(preview, SIGNAL(paintRequested(QPrinter*)), report, SLOT(Print(QPrinter*)));

    and you then print what you want in Print(..) ( can be named anything)

    Note that you can ask a widget to paint somewhere else using the render() method.

  • This post is deleted!

  • @mrjj solved, but the size of the preview i can't change it and is in the same line as the toolbar from where i do the printaction, where could be the problem?

  • Qt Champions 2017

    The widgets render in the size they have/are.
    If you want it bigger, you could print to image and scale it.
    You can maybe even do it directly with render and painter.scale but
    i never tested this.

    void MainWindow::printshot() {
      QPixmap pix = QPixmap::grabWindow(QApplication::desktop()->winId());
      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); // note uses the form width/height! use pix.h/w if random image
      painter.drawPixmap(0, 0, pix);

    If you need very high fidelity, split your paintEvent into a new function taking a QPainter *
    and call it for printing too. That way you can scale to use the full DPI of the printer.

    • and is in the same line as the toolbar from where i do the printaction
      I do not understand this line :)

  • @mrjj thank you so much but solved it last night, was just a problem when i was using the constructor for the printpreview

  • @mrjj but question, when i trigger print i should get an sample.pdf saved somewherE?

  • Qt Champions 2017

    yes if u set the filename and type to pdf

  • @mrjj , i set all this in an action print preview where i have also preview->print;
    preview->show . but i dont get an pdf saved nowhere

  • Qt Champions 2017

    @mrjj said in print preview widget:


    you control where with setOutputFileName
    make sure to use / and not \ for the full path to the file

  • void MainWindow::on_actionPrint_triggered()
    QPrinter *printer = new QPrinter(QPrinter::HighResolution);


    QPrintPreviewWidget *preview =0;
    preview = new QPrintPreviewWidget(printer,preview);


    connect(preview, SIGNAL(paintRequested(QPrinter*)),allWorkspaceShapes[currentIndex]->drawShape,SLOT(Print(QPrinter*)));



    i used this and still didn't get anything, i have another problem in the code maybe?

  • Qt Champions 2017

    @rest said in print preview widget:
    That is NOT a valid path

    Please use a valid full path and only single /


  • printer->setOutputFileName("C:/Users/admin/Desktop/test.pdf");
    i tried and nothing :(

  • Qt Champions 2017


    and you do have write access there ?
    and the path IS real. meaning u didnt make up the admin name.

  • @mrjj and then i used this and still does not work
    printer->setOutputFileName("C:/Users/Public/Public Documents/test.pdf");

  • Qt Champions 2017

    and it does show stuff in preview ?
    but the file is NOT created?

  • exactly, it shows but it's not saved

  • Qt Champions 2017

    and u press print inside preview ?
    to make it actually print and not just preview ?

  • @mrjj solved, i had make the print preview and printer object in the constructor of the window

  • Qt Champions 2017

    oh, so moving them helped?

  • @mrjj yes, thanks

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.