コード例 #1
0
 def get_xy_for_word(text: QTextEdit, word: str) -> QPoint:
     """
     Returns the pixel coordinates of the word on the QTextEdit canvas.
     If QTextEdit does not contain the specified word or it is invisible,
     returns 0, 0.
     """
     _text = text.toPlainText()
     x, y = 0, 0
     if word and word in _text:
         idx = _text.index(word)
         cursor = text.textCursor()
         cursor.movePosition(QTextCursor.Start)
         for _ in range(idx):
             cursor.movePosition(QTextCursor.NextCharacter)
         rect = text.cursorRect(cursor)
         x = rect.x() + rect.width()
         y = rect.y() + rect.height() // 2
         if x < 0 or y < 0:  # pragma: no cover
             x, y = 0, 0
     return QPoint(x, y)