Search code examples
pythonpyqtpyqt5qtwebengine

Spell checking in PyQtWebEngine


I am writing a program which is mainly in python but some interactive features are done through a web-app that talks to flask. It would be nice to have the web-app inside the python program so I am looking at using PyQtWebEngine.

This works surprisingly well except that I cannot get spell checking to work. I have run

self.page().profile().setSpellCheckEnabled(True)
self.page().profile().setSpellCheckLanguages({"en-GB"})

from inside my child class of QWebEngineView, and I have checked isSpellCheckEnabled() is True.

I wonder if it cannot find the languages. No qWarning is detected which I would expect if it cannot find the dictionary. As suggested by the non-python example.

I have an en-GB.bdic which I copied from the Chromium hunspell git. I have tried putting the file at:

<directory_my_py_file_is_in>/qtwebengine_dictionaries/en-GB.bdic

When I run

app = QApplication(sys.argv)
print(app.applicationDirPath())

the result is

/usr/bin

so I tried

/usr/bin/qtwebengine_dictionaries/en-GB.bdic

This wouldn't have been OK because I cannot edit this location when the program is pip installed, but it was worth a try.

With the .bdic file in either place I never see any spell check feature.

Has anyone got spellchecking working in PyQtWebEngine? I have not been able to find much in the way of documentation.


Solution

  • Assuming that the .bdic are valid then I have established the path of the dictionaries through the environment variable QTWEBENGINE_DICTIONARIES_PATH, for example I have translated the official example into python with the following structure:

    ├── data
    │   ├── icon.svg
    │   ├── index.html
    │   ├── spellchecker.qrc
    │   └── style.css
    ├── dict
    │   ├── de
    │   │   ├── de-DE.aff
    │   │   ├── de-DE.dic
    │   │   └── README.txt
    │   └── en
    │       ├── en-US.aff
    │       ├── en-US.dic
    │       └── README.txt
    ├── main.py
    ├── spellchecker_rc.py
    ├── qtwebengine_dictionaries
    │   ├── de-DE.bdic
    │   └── en-US.bdic
    └── README.md
    

    main.py

    # ...
    CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
    os.environ["QTWEBENGINE_DICTIONARIES_PATH"] = os.path.join(
        CURRENT_DIR, "qtwebengine_dictionaries"
    )
    # ...
    

    Note: To get the bdic I have used the qwebengine_convert_dict tool executing:

    qwebengine_convert_dict dict/en/en-US.dic qtwebengine_dictionaries/en-US.bdic
    qwebengine_convert_dict dict/de/de-DE.dic qtwebengine_dictionaries/de-DE.bdic
    

    The complete code is here.