print preview widget
-
wrote on 10 Aug 2017, 16:43 last edited by
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();
} -
@rest said in print preview widget:
QPrintPreviewWidget
Hi
Sample here
https://stackoverflow.com/questions/3827873/how-to-embedded-the-print-preview-dialog-in-qtkey 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.
-
@rest said in print preview widget:
QPrintPreviewWidget
Hi
Sample here
https://stackoverflow.com/questions/3827873/how-to-embedded-the-print-preview-dialog-in-qtkey 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.
wrote on 10 Aug 2017, 19:44 last edited byThis 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?
Lifetime Qt Championwrote on 11 Aug 2017, 06:30 last edited by mrjj 8 Nov 2017, 06:31@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 :)
- and is in the same line as the toolbar from where i do the printaction
-
@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 :)
- and is in the same line as the toolbar from where i do the printaction
-
@mrjj thank you so much but solved it last night, was just a problem when i was using the constructor for the printpreview
-
@rest
yes if u set the filename and type to pdf
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(filename); -
@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 -
wrote on 17 Aug 2017, 06:34 last edited by
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?
-
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?
@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 -
wrote on 17 Aug 2017, 06:53 last edited by
printer->setOutputFileName("C:/Users/admin/Desktop/test.pdf");
i tried and nothing :( -
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");@rest
and it does show stuff in preview ?
but the file is NOT created? -
wrote on 17 Aug 2017, 09:19 last edited by
exactly, it shows but it's not saved
-
@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
@rest
oh, so moving them helped?
1/21