Create a PDF from Qt Creator
-
@TheCipo76
That looks very ok.
But no PDF (PrimoPDF.pdf) is produced ?Have a look at this code. it prints screen shots.
Looks very similar to yours.void printv2(QPixmap& pix) { QPrinter printer(QPrinter::HighResolution); printer.setOrientation(QPrinter::Landscape); printer.setOutputFormat(QPrinter::PdfFormat); printer.setPaperSize(QPrinter::A4); printer.setOutputFileName("e:/test.pdf"); QPainter painter; painter.begin(&printer); double xscale = printer.pageRect().width() / double(pix.width()); double yscale = printer.pageRect().height() / double(pix.height()); double scale = qMin(xscale, yscale); painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2, printer.paperRect().y() + printer.pageRect().height() / 2); painter.scale(scale, scale); painter.translate(-pix.width() / 2, -pix.height() / 2); painter.drawPixmap(0, 0, pix); painter.end(); } void MainWindow::on_pushButton_released() { QPixmap pix = QPixmap::grabWindow(QApplication::desktop()->winId()); printv2(pix); }
-
@TheCipo76
Yep and i get a e:/test.pdf so not really sure why you dont get any.
does it try to print to real printer ?Can you try
void printv3() { QPrinter printer(QPrinter::HighResolution); printer.setOrientation(QPrinter::Landscape); printer.setOutputFormat(QPrinter::PdfFormat); printer.setPaperSize(QPrinter::A4); printer.setOutputFileName("test.pdf"); QPainter painter; painter.begin(&printer); painter.drawText( 200,200, "HELLO" ); painter.end(); }
That give me a test.pdf in the build folder
and expected result
-
@TheCipo76
Really odd. I cant guess sorry. Sample works here.
You use the code directly as is ? -
@mrjj Yes, copied and pasted
QPrinter printer(QPrinter::HighResolution); printer.setOrientation(QPrinter::Landscape); printer.setOutputFormat(QPrinter::PdfFormat); printer.setPaperSize(QPrinter::A4); printer.setOutputFileName("test.pdf"); QPainter painter; painter.begin(&printer); painter.drawText( 200,200, "HELLO" ); painter.end();
-
and you also searched for test.pdf to make sure
its not created any where else ? -
@TheCipo76
Very odd.
Can you try this code. it let u select save location
if that dont work. something up with your Qt.void printv4() { QPrinter printer(QPrinter::HighResolution); printer.setOrientation(QPrinter::Landscape); printer.setOutputFormat(QPrinter::PdfFormat); printer.setPaperSize(QPrinter::A4); QString fileName = QFileDialog::getSaveFileName(0,"file", "c:\\", "pdf (*.pdf)"); printer.setOutputFileName(fileName); QPainter painter; painter.begin(&printer); painter.drawText( 200,200, "HELLO" ); painter.end(); }
-
Super :)
So on mac it seems the file go somewhere else
if no path. maybe a read only place.
So when we tell the exact location , it does work :) -
@TheCipo76 said in Create a PDF from Qt Creator:
printer.setOutputFileName("PrimoPDF.pdf");
When launched from QtCreator, the default working directory is set to:
MyApp/Contents/MacOS/When launched from the finder, the path is usely the user directory, but actualy it is the root directory (/) and obviously nothing is written (wrong permissions)
-
@mrjj said in Create a PDF from Qt Creator:
Super :)
So on mac it seems the file go somewhere else if no pathIIRC the standard path is
/Users/yourUserName/Library/Application Support
-
@dan1973 said in Create a PDF from Qt Creator:
I need to know how to draw lines in PDF document using Qt