Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QPdfWriter buggy?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QPdfWriter buggy?

Scheduled Pinned Locked Moved General and Desktop
14 Posts 3 Posters 5.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yodermk
    wrote on last edited by
    #1

    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...

    1. 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

    2. 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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Not an answer, but did you check that newPage returns true ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        yodermk
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          yodermk
          wrote on last edited by
          #4

          Tried to find it in the source code but didn't have too much luck. :/

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            I would check whether isActive returns true and that you can really access the file you are trying to write

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • Y Offline
              Y Offline
              yodermk
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Strange indeed, can you write a little unit test with QPdfWriter failing ? So I can test on my side

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • Y Offline
                  Y Offline
                  yodermk
                  wrote on last edited by
                  #8

                  Yeah, give me a few days....

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    JvdGlind
                    wrote on last edited by
                    #9

                    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;
                    

                    }
                    @

                    Jeffrey VAN DE GLIND
                    Principle Consultant @ Nalys
                    www.nalys-group.com

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      JvdGlind
                      wrote on last edited by
                      #10

                      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;
                      

                      }
                      @

                      Jeffrey VAN DE GLIND
                      Principle Consultant @ Nalys
                      www.nalys-group.com

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        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

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • Y Offline
                          Y Offline
                          yodermk
                          wrote on last edited by
                          #12

                          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?

                          1 Reply Last reply
                          0
                          • Y Offline
                            Y Offline
                            yodermk
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              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

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved