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.
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 usingviewport()
and map the local position to global usingviewport()->mapToGlobal()
.I have found similar query to your in some online forum and here's someone suggested the output as
Note: Reference from http://www.unix.com/unix-linux-applications/81388-read-position-mouse-cursor.html, Author of below posting is daggilli, registered user of UNIX online forums. Credit of below posting in its complete form goes to daggilli.
This is the complete code for a Qt application I threw together in about ten minutes (called crosshair) which displays the current mouse coordinates in a window. You might be able to pull enough out of it to be useful. This is Qt 3.1, but Qt 4 is not a great deal different. You will need the Qt development libraries, not just the runtimes. The code comprises two files, crosshair.h and crosshair.cpp.
crosshair.h:
Code:
crosshair.cpp:
Code:
To build this, put both files in a directory called crosshair. cd to that directory and type Code:
This does nothing more complex than inherit from a QLabel, set a timer to run 20x a second, grab the current cursor coordinates and write them into the label's text. Clicking in the window closes it. I use it for fixing up alignment bugs in JavaScript when I'm laying out objects.
You could open a file in the Crosshair class's constructor to store your data, and use gettimeofday(2) to get a timestamp. Nothing says Qt has to run in GUI mode (you can tell it explicitly not to in the QApplication constructor).
Qt from Trolltech: http://doc.trolltech.com