Unsolved QPrintPreviewDialog print all pages though page range is selected
-
Version: QT Qt5.7.1_2013
I used QPrintPreviewDialog to preview whole printed document which is render in preview(QPrinter*)
connect(&previewDialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(preview(QPrinter*)));
then , I use the provided print button to print specified page of the document (eg 1 of 5 pages). But it always print all the pages.
-
Hi and welcome to devnet forum
The QPrintDialog functionality is typically using the native features of your OS. Therefore, it is outside of Qt.
You would need to check if the same issue is occuring while using the same procedure with other applications using the native print dialog from your OS. -
I encountered same issue (it is mentioned even in official QT doc, that this functionality depends on operating system). However there is a way to workaround it.
The idea is to create new QPrinter object, also with new QPainter. Then you pass them the print settings catched in preview dialog and render only desired pages from your data source, for example:
rangePainter.begin(pageRangePrinter); for(int i = from; i <= to; i++) { yourDataSource[i-1]->view->render(&rangePainter); if (i != printer->toPage() ) { pageRangePrinter->newPage(); } } rangePainter.end();
So in fact, you print using another QPrinter object.
However there is an important point to overcome. If user clicks "Print" in preview dialog, signal paintRequested() is emitted 2nd time and probably your logic showing preview before, will fire itself again and the printer will start printing all pages + pages in range (from logic above). I think there are many ways to manage with it, but one is to disconnect this signal after 1st emit (1st emit is being done when user request to show preview dialog).
I hope it will help