PyQt - 如何打开/关闭拼写检查突出显示
我有一个按钮,可以在 QTextEdit 框中设置/取消设置拼写检查突出显示(参考 PyQt -如何打开/关闭拼写检查)效果很好。
然后我添加了一个语言选择 QComboBox 并将其信号绑定到按钮的属性,但其突出显示设置/取消设置不适用于更改语言。这让我发疯,我可能做了一些小而愚蠢的事情,但为了它我找不到任何问题。
按钮(而是操作)是
self.actionSpellCheck = QAction(QIcon(self.icon_spellcheck),
"Auto &Spellcheck", self,
shortcut=Qt.CTRL + Qt.SHIFT + Qt.Key_O,
triggered=self.spellcheck, checkable=True)
组合框,
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)
而 self.spellcheck 是
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()
如何在单击按钮时设置/取消设置荧光笔,但选择语言时没有任何反应(它仅在我开始输入后发生,而不是在组合框选择时立即发生) )?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看
HighLighter.setDict
方法,您会发现它除了重新分配dict
属性之外没有做太多事情。此外,
SpellTextEdit.setHighlighterEnabled
仅重置文档。因此,当
dict
发生变化时,您将需要一种方法来重新突出显示文本。幸运的是,HighLighter
是QSyntaxHighlighter
的子类,它已经具有rehighlight
插槽可以完成所需的操作。因此,您只需修改您的
spellcheck
方法,如下所示:If you look at the
HighLighter.setDict
method, you'lll see that it doesn't do much other than reassign thedict
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 ofQSyntaxHighlighter
, which already has arehighlight
slot which does what is required.So you just need to amend your
spellcheck
method as follows: