[PyQt] Error in QTextBrowser.cursorRect()

Ryan Ofarrell john.ofarrell at ansys.com
Thu Aug 4 20:58:01 BST 2016


Hi

I am trying to test my pyqt gui application which has a QTextBrowser with
hyper link anchors in it. I need to figure out a way to click on these
anchors (preferably using QTest). To do this I decided to save the anchor
location each time I created one using QTextBrowser.cursorRect() however
this function is broken. Below I have replicated the code using QTextEdit():

>>> ll = QtGui.QTextEdit()
>>> ll.cursorRect()
PyQt4.QtCore.QRect(0, 0, 1, 10)
>>> ll.textCursor().position()
0
True
>>> ll.insertPlainText('move the cursor')
>>> ll.cursorRect()
PyQt4.QtCore.QRect(112, 4, 1, 17)
>>> ll.textCursor().position()
15
>>> ll.append('<a href  =' +'hhee'+'>'+'hhee'+ '</a>')
>>> ll.textCursor().position()
20
>>> ll.cursorRect()
PyQt4.QtCore.QRect(37, 21, 1, 17)

As you can see cursorRect() works fine with QtGui.QTextEdit() I then ran
the same sequence using QTextBrowser() instead:

>>> ll = QtGui.QTextBrowser()
>>> ll.textCursor().position()
0
>>> ll.cursorRect()
PyQt4.QtCore.QRect(0, 0, 1, 10)
>>> ll.append('<a href  =' +'hhee'+'>'+'hhee'+ '</a>')
>>> ll.textCursor().position()
4
>>> ll.cursorRect()
PyQt4.QtCore.QRect(0, 0, 1, 10)
>>> ll.insertPlainText('move the cursor')
>>> ll.textCursor().position()
19
>>> ll.cursorRect()
PyQt4.QtCore.QRect(0, 0, 1, 10)

As you can adding text to a QTextBrowser updates textCursor().position but
doesn't change the position returned by cursorRect().

In conclusion cursorRect() appears to be broken for QTextBrowser. Could you
please tell me what I am doing wrong or fix this issue. Alternatively could
you recommend a better way I could click on every single anchor in my
QTextBrowser. I need to be able to do so to test my application

Thanks
John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160804/19fe9be7/attachment.html>


More information about the PyQt mailing list