[SOLVED] QPdfWriter buggy?
-
Hi,
Not an answer, but did you check that newPage returns true ?
-
I would check whether isActive returns true and that you can really access the file you are trying to write
-
It's definitely writing the PDF, and painter.isActive appears to be returning true.
Is anyone actually using this class? Googling provides very few examples.
Also I'm using the Arch Linux Qt packages. I wonder if they hosed something.
Maybe I'll wait for the Qt 5.3 release and try again. Any other ideas?
-
Strange indeed, can you write a little unit test with QPdfWriter failing ? So I can test on my side
-
I believe the problem is with the painter being created on the stack each call.
This works:
@void MainWindow::generateCalendar()
{
int currMonth;
QPdfWriter writer("/home/jvdglind/Calendar.pdf");
writer.setCreator("Libre Calendar Creator");
writer.setPageSize(QPagedPaintDevice::A4);
QPainter painter(&writer);
for (currMonth = 0; currMonth < ui->monthList->count(); currMonth++) {
writer.newPage();
months[currMonth].drawCalendarPage(&painter, writer.height(), writer.width());
qDebug("hello " + months[currMonth].text().toLatin1());
}
}@This creates 1 blank page and a page for each month. Of course I did some other changes as well, but I'll leave that up to you.
This one works as well:
@#include <QApplication>
#include <QPainter>
#include <QPdfWriter>int main(int argc, char *argv[])
{
QApplication app(argc, argv);QPdfWriter pdfOut(QStringLiteral("test2.pdf")); QPainter painter; painter.begin(&pdfOut); painter.setPen(QPen(Qt::black, 0.1, Qt::SolidLine)); painter.setBrush(QBrush(Qt::white)); painter.setWindow(0, 0, 70, 70); for(qint32 month = 1; month <= 12; month++) { for(qint32 i = 0; i < 70 ; i+=10) { painter.drawRect(i, 0, i+10, 10); } if(month != 12) pdfOut.newPage(); } painter.end(); return 0;
}
@ -
In addition. This one does not work and mimics the results seen in your application:
@#include <QApplication>
#include <QPainter>
#include <QPdfWriter>void drawPage(QPdfWriter *pdfOut)
{
QPainter painter;
painter.begin(pdfOut);painter.setPen(QPen(Qt::black, 0.1, Qt::SolidLine)); painter.setBrush(QBrush(Qt::white)); painter.setWindow(0, 0, 70, 70); for(qint32 i = 0; i < 70 ; i+=10) { painter.drawRect(i, 0, i+10, 10); }
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);QPdfWriter pdfOut(QStringLiteral("test2.pdf")); for(qint32 month = 1; month <= 12; month++) { drawPage(&pdfOut); if(month != 12) pdfOut.newPage(); } return 0;
}
@ -
Nice catch !
I knew that having multiple painters at the same time on a paint device is a no go but I missed that one. Sounds like the documentation might need an update
-
-
Ok, got it. Pen issues.
And it looks like the ability to set page orientation will be new in 5.3. Since that is coming out shortly, I guess I can wait. I wonder how people managed without it, however!Thanks much again guys. I don't know if you have commit access to the documentation, but adding some of this stuff to the QPdfWriter (or QPagedPaintDevice) page might help others avoid confusion.
-
Concerning the use of QPdfWriter (or misuse), you could open a documentation bug on the "bug tracker":http://bugreports.qt-project.org so it will get tracked