That's what I am doing. When Cancel is pressed the remaining pages are skipped and only those already painted are physically printed. There's nothing I can do about that.
When the same code is used on a print preview the preview dialog displays everything up to last processed page after the paintRequested() handler returns. If Cancel is pressed while the paginate is running the preview display a single blank page (as expected).
What the user expects is that pressing Cancel will terminate the preview process entirely... not displaying the preview with the half-finished output or a blank page. I cannot see a clean way to do this because the paintRequested() slot cannot return a userAborted value or the like.
The handler for the QPrintPreviewDialog paintRequested() signal and friends look like:
void LogView
::paintRequested(QPrinter * printer
) {
Q_ASSERT(printer);
m_printer = printer;
// Force some settings on the printer
m_printer->setFullPage(true);
m_printer
->setPageMargins
(0,
0,
0,
0,
QPrinter::Millimeter);
m_printer
->setOrientation
(QPrinter::Landscape);
// Ensure the geometry is right for the device
calcGeometry(m_printer);
QList< QList<int> > pages;
if (paginate(&painter, &pages))
printPages(&painter, pages);
}
void LogView::paintRequested(QPrinter * printer)
{
Q_ASSERT(printer);
m_printer = printer;
// Force some settings on the printer
m_printer->setFullPage(true);
m_printer->setPageMargins(0, 0, 0, 0, QPrinter::Millimeter);
m_printer->setOrientation(QPrinter::Landscape);
m_printer->setPageSize(QPrinter::A4);
QPainter painter(printer);
// Ensure the geometry is right for the device
calcGeometry(m_printer);
QList< QList<int> > pages;
if (paginate(&painter, &pages))
printPages(&painter, pages);
}
To copy to clipboard, switch view to plain text mode
and
void LogView
::printPages(QPainter *painter,
const QList< QList<int> > &pages)
{
int firstPage = m_printer->fromPage() - 1;
if (firstPage >= pages.size())
return;
if (firstPage == -1)
firstPage = 0;
int lastPage = m_printer->toPage() - 1;
if (lastPage == -1 || lastPage >= pages.size())
lastPage = pages.size() - 1;
int numPages = lastPage - firstPage + 1;
for (int i = 0; i < m_printer->numCopies(); ++i) {
progress.setLabelText(tr("Printing %1 pages (Copy %2)").arg(numPages).arg(i+1));
progress.setRange(0, numPages-1);
progress.setModal(true);
for (int j = 0; j < numPages; ++j) {
// Process events for responsivness
progress.setValue(j);
qApp->processEvents();
if (progress.wasCanceled()) {
break;
}
if (i != 0 || j != 0)
m_printer->newPage();
int index;
if (m_printer
->pageOrder
() == QPrinter::FirstPageFirst) { index = firstPage + j;
} else {
index = lastPage - j;
}
printPage(painter, pages[index], index + 1);
}
if (progress.wasCanceled())
break;
}
}
void LogView::printPages(QPainter *painter,
const QList< QList<int> > &pages)
{
int firstPage = m_printer->fromPage() - 1;
if (firstPage >= pages.size())
return;
if (firstPage == -1)
firstPage = 0;
int lastPage = m_printer->toPage() - 1;
if (lastPage == -1 || lastPage >= pages.size())
lastPage = pages.size() - 1;
int numPages = lastPage - firstPage + 1;
for (int i = 0; i < m_printer->numCopies(); ++i) {
QProgressDialog progress(this);
progress.setLabelText(tr("Printing %1 pages (Copy %2)").arg(numPages).arg(i+1));
progress.setRange(0, numPages-1);
progress.setModal(true);
for (int j = 0; j < numPages; ++j) {
// Process events for responsivness
progress.setValue(j);
qApp->processEvents();
if (progress.wasCanceled()) {
break;
}
if (i != 0 || j != 0)
m_printer->newPage();
int index;
if (m_printer->pageOrder() == QPrinter::FirstPageFirst) {
index = firstPage + j;
} else {
index = lastPage - j;
}
printPage(painter, pages[index], index + 1);
}
if (progress.wasCanceled())
break;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks