WebEngine Widgets Spellchecker Example¶
Integrates a spellchecker into a simple HTML form.
Spellchecker demonstrates how to integrate spellchecking support into an HTML form that enables users to submit spellchecked messages.
Running the Example¶
To run the example from Qt Creator , open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.
To be able to check the spelling, we need to provide the spellchecker with dictionaries. The Qt WebEngine spellchecker supports dictionaries provided by the Hunspell project on all platforms and native dictionaries provided by macOS. In this example, we want to support the English and German languages.
For Hunspell dictionaries to be supported they have to be compiled into a special binary format. A Hunspell dictionary consists of two files:
.dicfile that is a dictionary containing words for the language
.afffile that defines the meaning of special flags in the dictionary
These two files can be converted into the
bdic format by using the
qwebengine_convert_dict tool that is shipped together with Qt.
In this example, we are going to compile en_US and de_DE dictionaries. However, the real full dictionaries would take too much space for the purposes of this example. Therefore, we have created two dummy dictionaries that contain the following words and can be used to demonstrate the conversion process:
English dictionary: I, you, he, she, it, we, they, love, loves, qt
German dictionary: ich, du, er, sie, es, wir, ihr, sie, Sie, liebe, liebst, liebt, lieben, liebt, qt
Each word in a dictionary can be prefixed with
q. For more information about how to create
aff files, see the Hunspell dictionary file format specification in the Hunspell Project .
See the Spellchecker feature documentation for how dictionary files are searched.
We specify the QMAKE_EXTRA_COMPILERS parameter in the project file to add a conversion step to the build process:
To set up a dictionary, we run
qwebengine_convert_dict passing the file path of the dictionary
bdic files. The
aff file and optional
delta file are also picked up by the
convert process. The output
bdic file is placed into the qtwebengine_dictionaries local directory (or Resources directory), which the application binary will run from.
Setting the Spellchecker¶
The constructor of our class is trivial.
We define simple mapping between our dictionary filenames and the actual language display name. We will use that mapping to display names of dictionaries in the context menu. Spellchecking is disabled by default. Therefore we also enable spellchecker and set the English dictionary. When Qt WebEngine’s spellcheck service initializes, it will try to load the
bdict dictionaries and to check them for consistency. Any errors are logged by using the
Switching the Spellchecking Language¶
The current language used for spellchecking is defined per profile, and can get set using the QWebEngineProfile::setSpellCheckLanguage method. When the user clicks on an underlined misspelled word, the default context menu displays up to four suggestions. Selecting one will replace the misspelled word. We could reimplement a number of suggestions, by overriding
contextMenuEvent and using QWebEngineContextMenuData::spellCheckerSuggestions, but we will demonstrate how to add langague options in the context menu instead:
Above, we get the QWebEngineContextMenuData instance using the QWebEnginePage::contextMenuData method. We use it to be notified when the user clicks on an editable field and show the Check Spelling item in the context menu. Moreover, if spellchecking is enabled, we also add the Select Language submenu with the supported languages. When an action is triggered, we set the language with the QWebEngineProfile::setSpellCheckLanguage call.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.