【问题标题】:Selection of text in QTextBrowser在 QTextBrowser 中选择文本
【发布时间】:2014-10-08 17:29:59
【问题描述】:

我有一个QTextBrowser,当我选择其中的一部分文本时,我需要选择开始和结束的位置。我用mousePressEventmouseReleaseEvent 这样做,它可以工作,但高亮选择(深蓝色)没有出现。为什么?所以我看不到我选择的内容。

我尝试使用信号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()

【问题讨论】:

  • 您在什么意义上需要位置:屏幕坐标、文本索引或其他?您应该发布演示您正在尝试做什么的最少代码。
  • 没人知道吗?

标签: python pyqt4 qtextbrowser


【解决方案1】:

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

QTextBrowser.mousePressEvent(self, mouseEvent)

QTextBrowser.mouseReleaseEvent(self, mouseEvent)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 2016-01-16
    相关资源
    最近更新 更多