python获取文本光标_python – Pyqt:在光标下获取文本

不幸的是,我现在无法测试这一点,所以这是对你需要的最佳猜测.这是基于我编写的一些代码,它有一个文本字段,在您输入的工具提示中显示错误,但应该有效.

你已经有了代码来选择悬停的单词,你只需要在正确的位置输入工具提示.

textCursor = text.cursorForPosition(event.pos())

textCursor.select(QTextCursor.WordUnderCursor)

text.setTextCursor(textCursor)

word = textCursor.selectedText()

if meetsSomeCondition(word):

toolTipText = toolTipFromWord(word)

# Put the hover over in an easy to read spot

pos = text.cursorRect(text.textCursor()).bottomRight()

# The pos could also be set to event.pos() if you want it directly under the mouse

pos = text.mapToGlobal(pos)

QtGui.QToolTip.showText(pos,toolTipText)

我已经离开了meetCondition()和toolTipFromWord()由你填写,因为你没有描述那些,但它们在需要去的地方非常具有描述性.

关于在没有选择单词的情况下执行此操作的注释,最简单的方法是在选择新光标之前缓存光标然后再将其设置.您可以通过调用QTextEdit.textCursor()然后像以前一样设置它来完成此操作.

像这样:

oldCur = text.textCursor()

textCursor.select(QTextCursor.WordUnderCursor) # line from above

text.setTextCursor(textCursor) # line from above

word = textCursor.selectedText() # line from above

text.setTextCursor(oldCur)

# if condition as above

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值