PyQt5。如何在与ContextMenu连接的QTextEdit中获取光标位置?

时间:2018-07-27 14:33:21

标签: python pyqt pyqt5

我尝试在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())

但它不起作用。

0 个答案:

没有答案