1

Doing a spell checker (using PyQt5 for the interface). I check the sentence entered in the editor and misspelled words, I highlight in red.

Tell me how to do what when you hover over such a word with the mouse, below popup text appeared with correct options? Here is a sample spell check code

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui, QtWidgets



class Example(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Hello")

        self.editTextArea = QtWidgets.QPlainTextEdit(self)
        self.editTextArea.setGeometry(QtCore.QRect(10, 10, 250, 250))
        self.editTextArea.setObjectName("editTextArea")

        variants = ['hello', 'hello1', 'hello2']
        word = 'Hallo'
        text =  "The word %s is not spelled correctly! Variants in the variants array and need to be displayed when hovering over the red word of the mouse " % word
        text = text.replace(word, '<span style="text-decoration: underline; color: red">' + word + '</span>')
        self.editTextArea.appendHtml(text)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = Example()
    form.show()
    app.exec()

I would like that when you hover the mouse over the red word, a window pops up with the correct options from the variants array, and when you click on one of the correct words, the wrong one is replaced.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    Unfortunately, while your question seems reasonable and technically correctly asks just a single question, it actually raises more than one as what you want to achieve is *not* simple: Qt provides the [QSyntaxHighlighter](https://doc.qt.io/qt-5/qsyntaxhighlighter.html) class that can be used for your needs, but it requires a complete implementation in this case, as you requirement is to highlight a word that does *not* match correct ones, so you must provide a vocabulary of *all* valid words; after that you'll need to work with the underlying QTextDocument in order to allow the menu feature. – musicamante Feb 21 '21 at 12:16
  • 1
    Since you're a beginner, you need to do some research and get experienced before being able to *correctly* do what you do, so I suggest you to start by looking for examples and questions about spell checking and QSyntaxHighlighter (such as [this](https://stackoverflow.com/questions/36343172/pyqt4-spell-check-with-syntax-highlighter) or [this](https://stackoverflow.com/questions/8722061/pyqt-how-to-turn-on-off-spellchecking)). Finally, you should not use fixed geometries for child widgets, especially for QMainWindow: remove `setGeometry()` and use `self.setCentralWidget(self.editTextArea)`. – musicamante Feb 21 '21 at 12:20

0 Answers0