How to create a Print Preview Dialog for a method
-
Hi,
Is it possible to create a print preview dialog for a method that prints to a local printer. The print method does not display any GUI dialog for user to choose from available printers listed to be used. Below is the example I tried but it does not work. Please any assistance would be much appreciated. Thanks in advance.def printReport(self,printer): report details def printpreviewDialog(self): printer = QPrinter(QPrinter.HighResolution) previewDialog = QPrintPreviewDialog(printer, self) previewDialog.paintRequested.connect(self.printReport) previewDialog.exec_()
-
@LT-K101 said in How to create a Print Preview Dialog for a method:
but it does not work
In what way it does not work?
-
@LT-K101
And what iscanvas
, and what do you expect this to do? The docs for QPrintPreviewDialog::paintRequested(QPrinter *printer) tell youThe printer instance supplied is the paint device onto which you should paint the contents of each page, using the
QPrinter
instance in the same way as you would when printing directly. -
@LT-K101
Well, your current code does not presently do anything like that....You are inside a method of an already-created
QPrintPreviewDialog
, at thepaintRequested
stage. This is not where or for popping up dialogs, and you don't want some dialog there to view the PDF since you are already inside a previewer. -
@LT-K101 said in How to create a Print Preview Dialog for a method:
Below is the printReport method details.
No wonder there is nothing in preview dialog.
As documentation explains (see @JonB post) you need to use the provided printer to generate the preview... -
@jsulm Honestly, I'm lost since this is the first time trying to implement this, I was following some examples online which is getting me confused. Please how do I use the provided printer to generate the preview in my case. I know it easy for you and @JonB to do this.Please I need assistance with this. Thanks
-
@LT-K101 Did you check https://doc.qt.io/qt-5/qtprintsupport-index.html ?
-
@LT-K101 said in How to create a Print Preview Dialog for a method:
but I did not understand it
What exactly?
There is an example:printer.setOutputFileName("print.ps"); QPainter painter; painter.begin(&printer); for (int page = 0; page < numberOfPages; ++page) { // Use the painter to draw on the page. if (page != lastPage) printer.newPage(); } painter.end();
-
@JonB It's the C++ syntax that confuses me. Sorry for bothering you.Please below is a report method I want to preview and print. It works perfectly without a Dialog by saving to disk so I wanted to use QPrintPreviewDialog to let user preview the report before printing.
def printReport(self): filename, ok = QtWidgets.QFileDialog.getSaveFileName(self, 'Save file', '', 'Pdf files (*.pdf)') if ok: # Open local project project = valentina.project.connect('./temp.vsp') # Make report instance report = project.report(name='tempofficials_summary', dsn='sqlite://C:/Users/PC/Desktop/PROJECT/temp.db', query="SELECT firstname,surname,email,phone,address,dob, '{}' || image as full_path FROM temp_table WHERE id_no={}".format( 'C:/Users/LTK101/Desktop/PROJECT/img/', id)) # Print result as PDF report.printToDisk(filename)
-
@LT-K101 said in How to create a Print Preview Dialog for a method:
I wanted to use QPrintPreviewDialog to let user preview the report before printing.
Then please do what was suggested here. Something like (should not be difficult to translate it to Python):
def reportPreview(self, printer): QPainter painter; painter.begin(printer); for (int page = 0; page < numberOfPages; ++page) { // Use the painter to draw on the page. // Put here code to draw preview using painter if (page != lastPage) printer.newPage(); } painter.end(); def printpreviewDialog(self): printer = QPrinter(QPrinter.HighResolution) previewDialog = QPrintPreviewDialog(printer, self) previewDialog.paintRequested.connect(self.reportPreview) previewDialog.exec_()
-
@LT-K101 said in How to create a Print Preview Dialog for a method:
printToLocalPrinter()
which print to any default printerIf that accepts a
QPrinter
it can be sent to aQPrintPreviewDialog
. But it does not look like it can? Looks like it does something about the printer internally instead?If I had been asking for help on whatever a
valentina.project
is or whatever your "report"/"report writer" is, which provides theseprint...()
calls, I would have supplied a link reference to it, plus however it interacts with Qt. But you would like us to answer without having that. I'm not even sure it integrates with Qt, let aloneQPrinter
, if it is just some Python thing what Qt integration does it have?