How to create a Print Preview Dialog for a method
-
@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? -
@JonB Well the Valentina report is written with Qt I have been able to integrate with PyQt5. The developers have not written a Dialog for the print method yet, when I asked I was told I can use pyQt5 to create a custom Dialog so I was hoping
QPrintPreviewDialog
will do the magic for me. Below is the image they suggested it could be done using pyqt.
-
@LT-K101 said in How to create a Print Preview Dialog for a method:
Well the Valentina report is written with Qt I have been able to integrate with PyQt5.
Is it really that hard to provide a link to this thing if you want help with it, after being asked to do so several times? I just don't get why you don't....
A custom dialog to invite the user to print is not the same thing as a
QPrintPreviewDialog
to allow previewing of the output. -
@LT-K101
Seriously, in what way? Create aQDialog
to ask the user if they want to print, whether to file or physical printer? And/or maybe aQFileDialog
to let the user pick a file/location to save to?Or maybe that inbuilt Windows Print dialog is all user needs anyway, seems to offer print to printer or to file?
-
@JonB said in How to create a Print Preview Dialog for a method:
maybe that inbuilt Windows Print dialog is all user needs anyway, seems to offer print to printer or to file?
Creating the a Dialog to offer print to printer or to file which will connect to my print method is what I need please.
-
@LT-K101
You write a dialog to ask the user whether they want to print and if they say yes you callprintToLocalPrinter()
orprintToDisk()
or similar. I'm not writing that for you, it's a dialog, I have no idea what your issue is. You also already have aprintReport()
shown earlier which does agetSaveFileName()
and callsprintToDisk(filename)
, so I don't know what your problem is.