PYQT5 Open menu on cursor position in QTextEditor

46 views Asked by At

I have a QTextEditor which I would like to open a menu for user to choose when he press Ctrl+Space. I was able to capture the key events and create the menu and trigger it.

My problem is to open the menu on the text cursor position. How do i get the QPoint of the textcursor and not the mouse position?

1

There are 1 answers

1
mugiseyebrows On BEST ANSWER

QTextEdit.cursorRect returns a rectangle (in viewport coordinates) that includes the cursor.

You're probably imlementing autocompletion/intellisense, it which case I'd recommend using QCompleter instead of QMenu. Take a look at Custom Completer Example.

You can override QTextEdit.keyPressEvent or use eventFilter to capture Ctrl+Space shortcuts and call completer.complete(rect) to show popup.