Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find global position of text cursor?

Tags:

qt

qt4

I would like to execute a QMenu object at the position of text cursor in a QPlainTextEdit. My problem is that QTextCursor is only define by its position in the Text (index of the character).

How can I find global position of the QTextCursor? Should I use an other object than QTextCursor in order to find the position of the text cursor where I want to open my QMenu?

Thank you by advance.

like image 266
Patrice Bernassola Avatar asked Oct 17 '09 21:10

Patrice Bernassola


People also ask

How do I find the cursor position in text area?

If there is no selection, you can use the properties . selectionStart or . selectionEnd (with no selection they're equal). var cursorPosition = $('#myTextarea').

How do I get the cursor position in HTML?

If you ever had a specific case where you had to retrieve the position of a caret (your cursor's position) inside an HTML input field, you can do so using the selectionStart property of an input's value.

How do I get the cursor position in C++?

You get the cursor position by calling GetCursorPos . This returns the cursor position relative to screen coordinates. Call ScreenToClient to map to window coordinates.

How do I get cursor position in terminal?

At ANSI compatible terminals, printing the sequence ESC[6n will report the cursor position to the application as (as though typed at the keyboard) ESC[n;mR , where n is the row and m is the column.


1 Answers

I've never tried myself, but doesn't QPlainTextEdit::cursorRect() work? It should give you position of the cursor in viewport coordinates. You can then get the viewport using viewport() and map the local position to global using viewport()->mapToGlobal().

like image 63
Lukáš Lalinský Avatar answered Oct 04 '22 11:10

Lukáš Lalinský