path: root/src/widgets
diff options
authorBoris Dušek <>2015-03-17 23:03:23 +0100
committerBoris Dušek <>2015-03-25 09:48:14 +0000
commit93bb16384a8028b7a29935f155e92547c911d772 (patch)
tree2bcacf49b8ea727ade51ca336a89608dff18f794 /src/widgets
parent310b5e73f05a82b69f66a4d13504132664de1242 (diff)
Fix QAccessibleTextWidget::characterRect for off-cursor positions
Current code retrieved the character format at position where the cursor currently was, irrespective of the position actually passed to the characterRect function. This is now fixed and we retrieve the character format for the QTextFragment containing the specified offset. This fixes display of visual bounds around text by screen reader in some cases. E.g. on OS X, when searching text using VoiceOver, VoiceOver first queries the visual bounds of the found text (and thus also characterRect), and only then it moves cursor position to the found text. If before the change of position the cursor was on some text with different metrics (i.e. a bigger font), the visual bounds for the found text reflected those metrics, not the metrics of the actual found text for which the bounds were drawn. This resulted in smaller/bigger rectangles around the found text than was actually correct. Change-Id: Ie2a4dfc714504b7923cdaf8ff875c438aeccddee Reviewed-by: Jan Arve Sæther <>
Diffstat (limited to 'src/widgets')
1 files changed, 14 insertions, 1 deletions
diff --git a/src/widgets/accessible/qaccessiblewidgets.cpp b/src/widgets/accessible/qaccessiblewidgets.cpp
index 77958d65a5..a2bf14b25b 100644
--- a/src/widgets/accessible/qaccessiblewidgets.cpp
+++ b/src/widgets/accessible/qaccessiblewidgets.cpp
@@ -667,7 +667,20 @@ QRect QAccessibleTextWidget::characterRect(int offset) const
if (line.isValid()) {
qreal x = line.cursorToX(relativeOffset);
- QFontMetrics fm(textCursor().charFormat().font());
+ QTextCharFormat format;
+ QTextBlock::iterator iter = block.begin();
+ if (iter.atEnd())
+ format = block.charFormat();
+ else {
+ while (!iter.atEnd() && !iter.fragment().contains(offset))
+ ++iter;
+ if (iter.atEnd()) // newline should have same format as preceding character
+ --iter;
+ format = iter.fragment().charFormat();
+ }
+ QFontMetrics fm(format.font());
const QString ch = text(offset, offset + 1);
if (!ch.isEmpty()) {
int w = fm.width(ch);