With PySide6 getSelection returns an invalid QPdfSelection. No matter what arguments we pass to it.
We'd expect a valid QPdfSelection with a non-zero boundingRectangle.
How to reproduce it
We call getAllText on a given QPdfDocument and we get a valid QPdfSelection and its boundingRectangle as expected:
selection: QPdfSelection = document.getAllText(1)
print(selection.isValid(), selection.boundingRectangle())
# True PySide6.QtCore.QRectF(72.000000, 72.000000, 451.000000, 728.000000)
But when we call getSelection we get an invalid QPdfSelection with a zero-ed boundingRectangle. No matter what arguments we pass to it (see below).
In our example here we use the top_left and bottom_right QPointFs of the boundingRectangle of the QPdfSelection returned by getAllText above:
top_left: QPointF = selection.boundingRectangle().topLeft()
bottom_right: QPointF = selection.boundingRectangle().bottomRight()
selection: QPdfSelection = document.getSelection(current_page, top_left, bottom_right)
print(selection.isValid(), selection.boundingRectangle())
# False PySide6.QtCore.QRectF(0.000000, 0.000000, 0.000000, 0.000000)
Minimal Reproducible Example
pip install pyside6==6.9.2
from PySide6.QtCore import QPointF
from PySide6.QtPdf import QPdfDocument, QPdfSelection
document: QPdfDocument = QPdfDocument(None)
document.load("filename.pdf")
current_page: int = 1
print("# getAllText()")
selection: QPdfSelection = document.getAllText(current_page)
print(selection.isValid(), selection.boundingRectangle())
# returns as expected:
# True PySide6.QtCore.QRectF(72.000000, 72.000000, 451.000000, 728.000000)
top_left: QPointF = selection.boundingRectangle().topLeft()
bottom_right: QPointF = selection.boundingRectangle().bottomRight()
print(top_left)
# PySide6.QtCore.QPointF(72.000000, 72.000000)
print(bottom_right)
# PySide6.QtCore.QPointF(523.000000, 800.000000)
print("# getSelection()")
selection: QPdfSelection = document.getSelection(current_page, top_left, bottom_right)
print(selection.isValid(), selection.boundingRectangle())
# doesn't returns as expected:
# False PySide6.QtCore.QRectF(0.000000, 0.000000, 0.000000, 0.000000)
Tested on both Linux and Windows
- Linux 5.10.0-19-amd64 #1 SMP Debian 5.10.149-1 (2022-10-17) with Python 3.12.11, Qt: v 6.9.2, PyQt: v 6.9.2
- Linux Debian 12 6.1.0-22-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.94-1 (2024-06-21) with Python 3.11.2, Qt: v 6.9.2, PyQt: v 6.9.2
- Windows 10 10.0.19045 with Python 3.12.6, Qt: v 6.9.2, PyQt: v 6.9.2
Both with following python packages
pyside6==6.9.2
pyside6-addons==6.9.2
pyside6-essentials==6.9.2
shiboken6==6.9.2
getSelectionworks as expected, but the behaviour can be very unpredictable. It seems that the underlying chrome pdf-engine doesn't always create the text-rects and map them to the view in the way it should.