python 光标位置输入文字_python – Pyqt:在光标下获取文本

如何获取光标下的文本?因此,如果我将鼠标悬停在它上面并且单词“hi”,我可以阅读它吗?我想我需要用QTextCursor.WordUnderCursor做一些事情,但我不确定是什么.有帮助吗?

这就是我现在正在努力解决的问题:

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

textCursor.select(QTextCursor.WordUnderCursor);

text.setTextCursor(textCursor);

word = textCursor.selectedText();

我现在正在选择文本,所以我可以看到它.

编辑2:

我真正想要做的是在文本中的某些单词上显示工具提示.

解决方法:

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

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

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

标签:python,user-interface,qt,pyqt

来源: https://codeday.me/bug/20190529/1176481.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值