QPdfView : Why doessn't this work????
-
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
-
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
-
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 ? -
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
-
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!
-
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.
-
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
-
Nice !
I would suggest you open a ticket (or maybe comment on the one existing) to discuss the documentation issue. -
D dullfig has marked this topic as solved on