1

我正在使用“PyQt4”开发桌面应用程序。该应用程序包含一个在 Qscintilla 之上实现的 XML 编辑器。但是,每当我单击通过指示器定义的类似超链接的文本时,我都会遇到问题。调用了“indicatorClicked”事件,但是当我在其中执行“SCI_GOTOLINE”API 时,它会正确地转到所需的行,但不幸的是,由于某种原因,它会从单击的文本位置选择文本直到目标行。对我来说,好像鼠标没有被释放!我也尝试使用“indicatorReleased”事件,但没有成功!你知道如何解决这个问题吗?

这就是我挂钩指示器释放事件的方式:

self.__editor.indicatorReleased.connect(self.__on_indicator_released)

事件处理程序只是将 SCI_GOTOLINE API 调用到某个行号:

 def __on_indicator_released(self, line_number, index, keys):
    self.__editor.SendScintilla(QsciScintilla.SCI_GOTOLINE, line_number)
4

1 回答 1

1

我在另一个线程上收到了 @Matic Kukovec 和 @K.Mulier 的回复。我在这里发布它,以便其他人可以从中受益。

@Matic Kukovec 回复我:“有两种解决方案:在 indicatorReleased 事件中使用 QTimer.singleShot 在短暂延迟(我使用 50 毫秒)后执行 SCI_GOTOLINE,或者在 indicatorReleased 事件中使用:while mouse_state != data.Qt .NoButton: PyQt5.QTest.qWait(1) 等到没有按下muse按钮,然后执行SCI_GOTOLINE。”

在尝试了这两种方法之后,基于 singleShot 的解决方案起到了魅力

挂钩到 indicator_released 处理程序:

self.__editor.indicatorReleased.connect(self.__on_indicator_released)

对应的事件处理程序:

def __on_indicator_released(self, line_number, index, keys):
    QTimer.singleShot(50, lambda: self.__go_to_line(line_number))

在 __go_to_line API 中调用 SCI_GOTOLINE:

def __go_to_line(self, line_number):
    self.__editor.SendScintilla(QsciScintilla.SCI_GOTOLINE, line_number)

但是,基于 mouse_state 的解决方案不起作用,因为 mouse_button 一直等于“Qt.NoButton”。

mouse_state = QtCore.QCoreApplication.instance().mouseButtons()
while mouse_state != Qt.NoButton:
    mouse_state = QtCore.QCoreApplication.instance().mouseButtons()
    QTest.qWait(1)
于 2019-12-17T19:36:04.307 回答