[SOLVED] QPdfWriter buggy?
-
Hi,
Trying to make a photo calendar generator which outputs PDF files. I have some code on GitHub, but it's vastly pre-alpha. There is a loop that goes through the months of the calendar, calling newPage() after every one, but there are problems...
-
The output PDF only has one page. The text on the page is the last month on the list. I know it is going through the loop and calling newPage each time, because the debug output is printed for each month. See this code:
https://github.com/yodermk/LIbreCalendarCreator/blob/master/mainwindow.cpp#L59-L62 -
The month text appears, but none of the lines. The on-screen widget rendering of this does show lines, but they are absent in the PDF. See this code:
https://github.com/yodermk/LIbreCalendarCreator/blob/master/monthclass.cpp#L36-L43
Kind of driving me nuts. Any idea what's going on?
Thanks! -
-
Hi,
Not an answer, but did you check that newPage returns true ?
-
Hmm. It did indeed return false. But why? Nothing in the documentation that I've seen gives any hint as to why it would fail!
Thanks.
-
Tried to find it in the source code but didn't have too much luck. :/
-
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
-
Yeah, give me a few days....
-
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
-
Thanks much JvdGlind - definitely a winner for the newPage mystery!
The lines still aren't showing up on the PDF, but let me play with this some more....
Also, does anyone know how to output the PDF in landscape orientation instead of portrait?
-
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