PyQt - 如何打开/关闭拼写检查突出显示

发布于 2024-12-25 03:03:33 字数 1546 浏览 5 评论 0原文

我有一个按钮,可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑疤怪 2025-01-01 03:03:33

如果您查看 HighLighter.setDict 方法,您会发现它除了重新分配 dict 属性之外没有做太多事情。

此外,SpellTextEdit.setHighlighterEnabled 仅重置文档。

因此,当 dict 发生变化时,您将需要一种方法来重新突出显示文本。幸运的是,HighLighterQSyntaxHighlighter 的子类,它已经具有 rehighlight 插槽可以完成所需的操作。

因此,您只需修改您的 spellcheck 方法,如下所示:

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:
        ...

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:
        ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文