Reputation: 1447
I have a button which sets/unsets spellcheck highlighting in a QTextEdit box (ref PyQt - How to turn on/off spellchecking) which works fine.
Then I added a language selection QComboBox and tied its signal to the button's property but its highlighting set/unset doesn't work on changing the language. It drives me nuts, there may be something small and stupid I've done, but for the sake of it I can't find anything wrong with it.
The button (action rather) is
self.actionSpellCheck = QAction(QIcon(self.icon_spellcheck),
"Auto &Spellcheck", self,
shortcut=Qt.CTRL + Qt.SHIFT + Qt.Key_O,
triggered=self.spellcheck, checkable=True)
The combobox is
self.cb_lang = QComboBox(tb)
tb.addWidget(self.cb_lang)
lang_list = self.dict_broker.list_languages()
self.cb_lang.addItems(lang_list)
self.cb_lang.currentIndexChanged.connect(self.spellcheck)
and the self.spellcheck is
def spellcheck(self):
pos = self.cursor.position()
if self.actionSpellCheck.isChecked():
lang = self.cb_lang.currentText()
self.dict = self.dict_broker.request_dict(lang)
self.highlighter.setDict(self.dict)
self.setHighlighterEnabled(True)
self.show_status("Spellcheck language is set to " + self.dict.tag, None)
else:
self.setHighlighterEnabled(False)
self.highlighter.setDict(None)
self.show_status("Spellcheck is turned off", None)
self.cursor.setPosition(pos, QTextCursor.MoveAnchor)
self.textEdit.setTextCursor(self.cursor)
self.textEdit.setFocus()
How come the highlighter gets set/unset on clicking the button, but nothing happens on selecting the language (it only happens after I start typing, not immediately on combobox selection)? Thank you.
Upvotes: 0
Views: 1217
Reputation: 120738
If you look at the HighLighter.setDict
method, you'lll see that it doesn't do much other than reassign the dict
attribute.
Also, the SpellTextEdit.setHighlighterEnabled
only resets the document.
So you're going to need a method to re-highlight the text whenever the dict
changes. Fortunately, HighLighter
is a subclass of QSyntaxHighlighter
, which already has a rehighlight
slot which does what is required.
So you just need to amend your spellcheck
method as follows:
def spellcheck(self):
pos = self.cursor.position()
if self.actionSpellCheck.isChecked():
self.setHighlighterEnabled(True)
lang = self.cb_lang.currentText()
self.dict = self.dict_broker.request_dict(lang)
self.highlighter.setDict(self.dict)
self.highlighter.rehighlight()
else:
...
Upvotes: 1