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. QPdfWriter and Qpainter Position
Qt 6.11 is out! See what's new in the release blog

QPdfWriter and Qpainter Position

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.4k Views
  • 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.
  • J Offline
    J Offline
    Jun_
    wrote on last edited by
    #1

    I am using QPdfWriter to output pdf files and Qpainter to draw. The dpi affects the coordinate system of Qpainter, but I don't know how the coordinate system of Qpainter is converted.

    	QPdfWriter *pdfWriter = new QPdfWriter(&pdfFile);             
    	pdfWriter->setPageSize(QPagedPaintDevice::A4);              
    	pdfWriter->setResolution(QPrinter::ScreenResolution);           
    	pdfWriter->setPageMargins(QMarginsF(0, 0, 0, 0));
            QTextOption option(Qt::AlignCenter);                           
          	option.setWrapMode(QTextOption::WordWrap);       
            QFont font("Arial", 12);
            QPainter *pdfPainter = new QPainter(pdfWriter); 
            pdfPainter->setFont(font);
            
            pdfPainter->drawText(QRect(500, 500, 2500, 500), "test", option);
    

    The text should be drawn at x:500 y:500, but when I open the pdf file it is not at that position. How is this conversion done? How can I ensure that it is drawn at the correct position?

    1 Reply Last reply
    0
    • J Jun_

      @Bonnie Thanks for your reply.
      What I mean is how to convert x, y, width, and height under the influence of different DPI.
      exp:
      pdfWriter->setResolution(96);
      QTextOption option(Qt::AlignLeft);
      pdfPainter->drawText(QRect(40, 40, 2500, 500), "test", option);
      Can be drawn correctly at 40,40

      pdfWriter->setResolution(QPrinter::ScreenResolution);
      QTextOption option(Qt::AlignLeft);
      pdfPainter->drawText(QRect(x, y, width, height), "test", option);
      x, y How to convert to achieve the same position effect as 40

      B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #4

      @Jun_ Actually I don't quite understand what is "correctly at 40,40". When setting 96dpi, drawing at 40,40 just means drawing at 0.42inch, 0.42inch.

      pdfWriter->setResolution(QPrinter::ScreenResolution);

      I don't know why are you keep writing like ⬆this, as I said this is invalid, it equals

      pdfWriter->setResolution(0);
      

      Anyway after calling that, pdfWriter->resolution() tells me the current resolution is 1200.
      For 1200dpi, to draw at the same position as 96dpi, every value should * 12.5.
      So 500,500 is the same position as 40,40 on 96dpi.

      pdfWriter->setResolution(96);
      pdfPainter->drawText(QPoint(40, 40), "test");
      
      pdfWriter->setResolution(1200);
      pdfPainter->drawText(QPoint(500, 500), "test");
      
      J 1 Reply Last reply
      1
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by Bonnie
        #2

        pdfWriter->setResolution(QPrinter::ScreenResolution);

        This is wrong and invalid (because QPrinter::ScreenResolution = 0), so it will keep the default resolution which is 1200(dpi).
        So for A4 size (8.26inch x 11.69inch), that will be about 9912 x 14028.

        QTextOption option(Qt::AlignCenter);

        You draw the text in the center of QRect(500, 500, 2500, 500), so the text won't be at x:500 y:500.
        If you want to test the coordinate, draw a rect by

        pdfPainter->drawRect(QRect(500, 500, 2500, 500));
        
        J 1 Reply Last reply
        0
        • B Bonnie

          pdfWriter->setResolution(QPrinter::ScreenResolution);

          This is wrong and invalid (because QPrinter::ScreenResolution = 0), so it will keep the default resolution which is 1200(dpi).
          So for A4 size (8.26inch x 11.69inch), that will be about 9912 x 14028.

          QTextOption option(Qt::AlignCenter);

          You draw the text in the center of QRect(500, 500, 2500, 500), so the text won't be at x:500 y:500.
          If you want to test the coordinate, draw a rect by

          pdfPainter->drawRect(QRect(500, 500, 2500, 500));
          
          J Offline
          J Offline
          Jun_
          wrote on last edited by
          #3

          @Bonnie Thanks for your reply.
          What I mean is how to convert x, y, width, and height under the influence of different DPI.
          exp:
          pdfWriter->setResolution(96);
          QTextOption option(Qt::AlignLeft);
          pdfPainter->drawText(QRect(40, 40, 2500, 500), "test", option);
          Can be drawn correctly at 40,40

          pdfWriter->setResolution(QPrinter::ScreenResolution);
          QTextOption option(Qt::AlignLeft);
          pdfPainter->drawText(QRect(x, y, width, height), "test", option);
          x, y How to convert to achieve the same position effect as 40

          B 1 Reply Last reply
          0
          • J Jun_

            @Bonnie Thanks for your reply.
            What I mean is how to convert x, y, width, and height under the influence of different DPI.
            exp:
            pdfWriter->setResolution(96);
            QTextOption option(Qt::AlignLeft);
            pdfPainter->drawText(QRect(40, 40, 2500, 500), "test", option);
            Can be drawn correctly at 40,40

            pdfWriter->setResolution(QPrinter::ScreenResolution);
            QTextOption option(Qt::AlignLeft);
            pdfPainter->drawText(QRect(x, y, width, height), "test", option);
            x, y How to convert to achieve the same position effect as 40

            B Offline
            B Offline
            Bonnie
            wrote on last edited by Bonnie
            #4

            @Jun_ Actually I don't quite understand what is "correctly at 40,40". When setting 96dpi, drawing at 40,40 just means drawing at 0.42inch, 0.42inch.

            pdfWriter->setResolution(QPrinter::ScreenResolution);

            I don't know why are you keep writing like ⬆this, as I said this is invalid, it equals

            pdfWriter->setResolution(0);
            

            Anyway after calling that, pdfWriter->resolution() tells me the current resolution is 1200.
            For 1200dpi, to draw at the same position as 96dpi, every value should * 12.5.
            So 500,500 is the same position as 40,40 on 96dpi.

            pdfWriter->setResolution(96);
            pdfPainter->drawText(QPoint(40, 40), "test");
            
            pdfWriter->setResolution(1200);
            pdfPainter->drawText(QPoint(500, 500), "test");
            
            J 1 Reply Last reply
            1
            • B Bonnie

              @Jun_ Actually I don't quite understand what is "correctly at 40,40". When setting 96dpi, drawing at 40,40 just means drawing at 0.42inch, 0.42inch.

              pdfWriter->setResolution(QPrinter::ScreenResolution);

              I don't know why are you keep writing like ⬆this, as I said this is invalid, it equals

              pdfWriter->setResolution(0);
              

              Anyway after calling that, pdfWriter->resolution() tells me the current resolution is 1200.
              For 1200dpi, to draw at the same position as 96dpi, every value should * 12.5.
              So 500,500 is the same position as 40,40 on 96dpi.

              pdfWriter->setResolution(96);
              pdfPainter->drawText(QPoint(40, 40), "test");
              
              pdfWriter->setResolution(1200);
              pdfPainter->drawText(QPoint(500, 500), "test");
              
              J Offline
              J Offline
              Jun_
              wrote on last edited by
              #5

              @Bonnie thanks a lot It is indeed a 12.5-fold relationship.

              1 Reply Last reply
              0
              • J Jun_ has marked this topic as solved on

              • Login

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