【问题标题】:Executing "SCI_GOTOLINE" API of Qscintilla inside indicatorReleased() event handler is not functioning properly在 indicatorReleased() 事件处理程序中执行 Qscintilla 的“SCI_GOTOLINE”API 无法正常运行
【发布时间】:2020-04-10 08:11:39
【问题描述】:

我正在使用“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)

【问题讨论】:

  • 问题得到解决,我在下面发布了解决方案。但是,我将编辑原始问题并粘贴代码状态 - 在修复之前
  • 很高兴您找到了答案,但提供 MRE 也很棒(对于未来的出版物),因为它可以帮助社区理解您的问题,因此更容易尝试帮助您。跨度>
  • 当然。我完全同意你的看法。感谢您的理解:)

标签: python pyqt pyqt4 qscintilla


【解决方案1】:

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

@Matic Kukovec 回复我:“有两种解决方案:在 indicatorReleased 事件中使用 QTimer.singleShot 在短暂延迟后执行 SCI_GOTOLINE(我使用50ms),或者在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)

【讨论】:

  • 嗨@Rida,我很高兴 Matic 解决了您的问题。感谢您在此处发布解决方案,以便其他人可以从中受益:-)
  • 你和 Matic 展示了我们应该如何造福他人的好例子。您正在以身作则
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-25
  • 2011-06-20
  • 2013-03-02
  • 1970-01-01
  • 2014-07-27
  • 2013-03-25
相关资源
最近更新 更多