0

我有一个QTextBrowser,当我选择里面的一部分文本时,我需要选择开始和结束的位置。我用mousePressEventand来做,mouseReleaseEvent它可以工作,但是高亮选择(深蓝色)没有出现。为什么?所以我看不到我选择了什么。

我尝试使用信号selectionChanged,但问题是每次选择文本时都会调用信号,而不是在释放鼠标和选择结束时调用。

有什么建议吗?

谢谢!

编辑:

这就像我想要的那样工作:

class MyBrowser(QTextBrowser):
    def __init__(self, parent=None, textableAnnotate=None):
        super(MyBrowser, self).__init__(parent)
        self.textableAnnotate   = textableAnnotate
        self.mousePress         = 0
        self.mouseRelease       = 0

    def mousePressEvent(self, mouseEvent):
        self.mousePress = self.cursorForPosition(mouseEvent.pos()).position()

    def mouseReleaseEvent(self, mouseEvent):
        self.mouseRelease = self.cursorForPosition(mouseEvent.pos()).position()

我需要点击的位置。这里:self.mousePress 和 self.mouseRelease。但是当我在 QTextBrowser 中选择文本时,突出显示不会出现。我希望我更清楚...

编辑2:

或这个:

self.browserInput.selectionChanged.connect(self.position)

def position(self):
        start = self.browserInput.textCursor().selectionStart()
        end   = self.browserInput.textCursor().selectionEnd()
4

1 回答 1

0

您需要将事件传递给正常的处理程序,因为您已经改变了正常的行为。在适当的事件处理程序的末尾添加以下行:

QTextBrowser.mousePressEvent(self, mouseEvent)

QTextBrowser.mouseReleaseEvent(self, mouseEvent)
于 2014-10-10T13:37:37.437 回答