View Full Version : QGraphicsTextItem ensure cursor visible

12th September 2011, 22:46

I have a very simple text editor based on QGraphicsTextItem. I would like to ensure that the blinking cursor is visible if I move the cursor. How can I get the actual cursor position? (The coordinates, not the character number)
I have implemented scroll bar in my editor so I can easily scroll to any part of the text, just I need to know the cursor's actual coordinate in the document. Is it possible?

(I cannot use QTextEdit in a proxy widget, but I think, what I need is a similar function to QTextEdit's ensurecursorvisible)

13th September 2011, 11:38
(I cannot use QTextEdit in a proxy widget,
Why not?

just I need to know the cursor's actual coordinate in the document.
Wont this do?:


13th September 2011, 20:53
Why not?
Because of performance problems and the boss don't want it :)

No, it just gives me the character number in the text. (e.g.: the cursor is at the 149th character in the document, but I don't know the coordinates of it)

14th September 2011, 10:10
Without using QTextEdit, I think you will have to go low level for that.

16th September 2011, 08:06
At last I found the way:

QTextBlock block = document()->findBlock(textCursor().position());
QTextLine line = block.layout()->lineForTextPosition(textCursor().positionInBlock() );
qreal cursor_y_position = document()->documentLayout()->blockBoundingRect(block).y() + line.y();
qreal line_height = line.height();
qDebug("New cursor position: %.2f %.2f", cursor_y_position, line_height);
if ( _actual_position > cursor_y_position )
if ((_actual_position + _height) < (cursor_y_position + line_height))
ScrollToPosition(cursor_y_position + line_height - _height);

21st August 2012, 20:01
Nice Code snippet, but what are actual_position and _height?

Does actual_position = textCursor().position() and _height = document().size().height()?