残念ながら、現時点ではこれをテストすることはできないため、これは何が必要かについての最良の推測です。これは、入力時にツールチップにエラーを表示するテキストフィールドを持つ、私が書いたいくつかのコードに基づいていますが、動作するはずです。
ホバー オーバーの下にある単語を選択するためのコードは既に用意されています。あとは、適切な場所にツールヒントが必要なだけです。
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)
あなたはそれらについて説明meetsSomeCondition()
し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