我尝试在QTextEdit
中获得光标位置:
class CustomEdit(QTextEdit):
def __init__(self):
super(CustomEdit, self).__init__()
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())
但它不起作用。