How to get FileSaveDialog with QPrintDialog?
-
Normally, in other apps when I click print:
after selecting
Microsoft Print to PDF
a file save dialog pops up automatically BUT with these:QPrinter printer; QPrintDialog dialog(&printer); if(dialog.exec() != QPrintDialog::Accepted) return; ...
I don't get that! How to get that file save dialog?
-
@Emon-Haque I do not have a Windows dev environment to hand. What happens if you do not do this?
printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName("test.pdf");
-
The Microsoft Print to PDF driver (not Qt) opens the Save dialog when the application (yours or anyone else's) prints to it and it has something to save. Your code snippet has chosen a printer but not actually printed anything.
@ChrisW67, I shortened that for question. The actual function is:
void QueryResultView::printTable(){ QPrinter printer; QPrintDialog dialog(&printer); if(dialog.exec() != QPrintDialog::Accepted) return; int row = resultModel->rowCount() + 1; int column = resultModel->columnCount(); printer.setPageSize(QPageSize::A4); printer.setPageMargins(QMarginsF(72,72,72,72), QPageLayout::Point); printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName("test.pdf"); // here I want to give the chosen file name/path but I don't get any file save dialog printer.setResolution(fontMetrics().fontDpi()); QTextDocument doc; doc.setPageSize(printer.pageRect(QPrinter::DevicePixel).size()); QTextCursor cursor(&doc); QTextTableFormat tableFormat; tableFormat.setBorder(0); tableFormat.setCellSpacing(0); tableFormat.setCellPadding(0); tableFormat.setHeaderRowCount(1); tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100)); cursor.insertTable(row, column, tableFormat); addHeader(cursor); for (int r = 1; r < row; r++) { for (int c = 0; c < column; c++) { auto index = table->model()->index(r, c); cursor.insertText( table->model()->data(index).toString() ); cursor.movePosition(QTextCursor::NextCell); } } doc.print(&printer); }
and if I omit
printer.setOutputFileName("test.pdf")
, in the Qt Creator output I getQPainter::begin(): Returned false
. -
@Emon-Haque I do not have a Windows dev environment to hand. What happens if you do not do this?
printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName("test.pdf");
-
@Emon-Haque I do not have a Windows dev environment to hand. What happens if you do not do this?
printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName("test.pdf");
@ChrisW67, yes, now I get that file save dialog. I also get this message in Qt Creator output
Invalid parameter passed to C runtime function.