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);
    printer->setOutputFormat(QPrinter::PdfFormat);
    printer->setOutputFileName("sample.pdf");
    printer->setPaperSize(QPrinter::A4);
    printer->setFullPage(true);
    printer->setResolution(300);
    QPrintPreviewWidget *preview = new QPrintPreviewWidget(printer,ui->drawGraphicView);
    preview->print();
    }


  • Qt Champions 2016

    @rest said in print preview widget:

    QPrintPreviewWidget

    Hi
    Sample here
    https://stackoverflow.com/questions/3827873/how-to-embedded-the-print-preview-dialog-in-qt

    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 2016

    @rest
    Super.
    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);
      printer.setOrientation(QPrinter::Landscape);
    
      QPainter painter;
      painter.begin(&printer);
      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);
      painter.end();
    }
    

    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 2016

    @rest
    yes if u set the filename and type to pdf
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName(filename);



  • @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 2016

    @mrjj said in print preview widget:

    printer.setOutputFileName(filename);

    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);
    printer->setOutputFormat(QPrinter::PdfFormat);

    printer->setPaperSize(QPrinter::A4);
    printer->setOrientation(QPrinter::Landscape);
    printer->setFullPage(true);
    //printer->setResolution(100);
    printer->setOutputFileName("Desktop//aaaa.pdf");

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

    preview->print();

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

    preview->show();

    }

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


  • Qt Champions 2016

    @rest said in print preview widget:
    That is NOT a valid path
    setOutputFileName("Desktop//aaaa.pdf");

    Please use a valid full path and only single /

    like
    C:/Users/SOMEUSERNAME/Desktop/test.pdf



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


  • Qt Champions 2016

    @rest

    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 2016

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



  • exactly, it shows but it's not saved


  • Qt Champions 2016

    @rest
    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 2016

    @rest
    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.