PyQt5. How to get cursor position in QTextEdit connected with ContextMenu?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


PyQt5. How to get cursor position in QTextEdit connected with ContextMenu?



I try to get cursor position in QTextEdit:


QTextEdit


class CustomEdit(QTextEdit):
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.cursor = self.textCursor()
self.menu = None
self.customContextMenuRequested.connect(self.create_menu)

def create_menu(self, pos):
self.menu = QMenu()
self.menu.addSeparator()
func = self.menu.addAction(u'function')
action = self.menu.exec_(self.viewport().mapToGlobal(pos))
if action == func:
self.func()

def func(self)
print(self.cursor.positionInBlock())



but it doesn't work.





What does it mean that it does not work?
– eyllanesc
21 mins ago





It returns wrong number that not correlated with cursor position
– tanitrielle
19 mins ago





Is that your real code? Why do you pass pos tofunc if that function does not require arguments?, what is self?
– eyllanesc
18 mins ago




pos


func


self





I correct copy past from project. Self - it is argument of this function as part of class that inherited from QTextEdit
– tanitrielle
15 mins ago





pyqt4 or pyqt5??
– eyllanesc
12 mins ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived