Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QPdfView : Why doessn't this work????
Forum Updated to NodeBB v4.3 + New Features

QPdfView : Why doessn't this work????

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 2 Posters 1.2k 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.
  • D Offline
    D Offline
    dullfig
    wrote on last edited by
    #1

    Hello everyone.

    QPdfView has a known bug, (there is an open ticket, not getting fixed anytime soon) If you set the QPdfView.setZoomMode to FitWidth, the reported zoom factor is incorrect. So I figured, fine, I'll calculate it myself. But if you try

    self.setZoomMode(QPdfView.ZoomMode.Custom)
    self.setZoomFactor( self.viewport().width() / self.document().pagePointSize( self.pageNavigator().currentPage())

    the value you get is wildly incorrect. I can't see how that is the case though, since you are trying to fit the whole page width into the widget, and you know the width of the document in points, and you know the width of the widget!! What am i not getting?

    Any help will be appreciated.

    Dan

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dullfig
      wrote on last edited by dullfig
      #6

      So for anyone running into the same problem, I figured it out: The documentation is incorrect, the zoom factor is NOT how many points per pixel. The width and length of the page are converted to logical dots per inch ( width / 72 * 96) and then the zoom factor is figured out from the ratio of the viewport to the page width in DPI!

       @Slot()
          def newDoc(self):
              if self.document() is not None:
                  dpi: float = QGuiApplication.screens()[0].logicalDotsPerInch()
                  self.setPageMode(QPdfView.PageMode.SinglePage)
                  self.setZoomMode(QPdfView.ZoomMode.Custom)
                  self.show()
      
                  page_width: float = self.document().pagePointSize(0).width() / 72 * dpi
                  view_width = self.viewport().width()
                  zoom_factor = view_width / page_width
                  self.pageNavigator().jump(0, QPoint(), zoom_factor)
                  self.setZoomFactor(zoom_factor)
              else:
                  self.hide()
      

      It came to me in a "AHA" moment.

      Dan

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

        Hi and welcome to devnet,

        Can you post the link to the ticket ?
        Which version of PySide6 or PyQt6 are you using ?
        Can you provide a complete script that shows your issue ?
        Do you have a reference PDF file that you can share so people can test in the same conditions as you ?

        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
        • D Offline
          D Offline
          dullfig
          wrote on last edited by dullfig
          #3

          ok, here is the bug report : [https://bugreports.qt.io/browse/QTBUG-119634]

          I'm using PySide6,

          here is the slot for new document:

          Slot()
          def newDoc(self):
                 if self.document() is not None:
                     self.setPageMode(QPdfView.PageMode.SinglePage)
                     self.setZoomMode(QPdfView.ZoomMode.Custom)
                     self.show()
                     
                     page_width: float = self.document().pagePointSize(0).width()
                     view_width = self.width()
                     zoom_factor = view_width / page_width
                     self.pageNavigator().jump(0, QPoint(), zoom_factor)
                     self.setZoomFactor(zoom_factor)
                 else:
                     self.hide()
          

          now keep in mind normally newDoc sets the zoom mode to FitWidth, but I am modifying it for testing. The calculated zoom factor is something like .31, should be closer to .25 with a viewport width of 800. The PDF I am trying to open has a width in points of 2448.0

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dullfig
            wrote on last edited by dullfig
            #4

            I.m trying to implement a "zoom to area" functionality, I got the rubber band working correctly, and with the zoom mode set to custom, it correctly calculates the new zoom factor. But it doesn't work at all if the zoom mode is fit to width or fit to screen.

            QPdfView OBVIOUSLY knows what the true zoom factor is (internally) or it wouldn't know how to display the PDF in "fit to screen" mode!

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

              There are several patches that have already been pushed to fix it why the latest is two weeks all, granted it's work in progress. You can check there what is used for the zoom factor.

              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
              • D Offline
                D Offline
                dullfig
                wrote on last edited by dullfig
                #6

                So for anyone running into the same problem, I figured it out: The documentation is incorrect, the zoom factor is NOT how many points per pixel. The width and length of the page are converted to logical dots per inch ( width / 72 * 96) and then the zoom factor is figured out from the ratio of the viewport to the page width in DPI!

                 @Slot()
                    def newDoc(self):
                        if self.document() is not None:
                            dpi: float = QGuiApplication.screens()[0].logicalDotsPerInch()
                            self.setPageMode(QPdfView.PageMode.SinglePage)
                            self.setZoomMode(QPdfView.ZoomMode.Custom)
                            self.show()
                
                            page_width: float = self.document().pagePointSize(0).width() / 72 * dpi
                            view_width = self.viewport().width()
                            zoom_factor = view_width / page_width
                            self.pageNavigator().jump(0, QPoint(), zoom_factor)
                            self.setZoomFactor(zoom_factor)
                        else:
                            self.hide()
                

                It came to me in a "AHA" moment.

                Dan

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

                  Nice !
                  I would suggest you open a ticket (or maybe comment on the one existing) to discuss the documentation issue.

                  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
                  1
                  • D dullfig 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