简体   繁体   English

如何在 PyQt5 中使“textEdit.find()”的选定文本变暗?

[英]How to darken selected text of `textEdit.find()` in PyQt5?

I have this method that finds a term in a textEdit:我有这个方法可以在 textEdit 中找到一个术语:

def search(self, term, case_sensitive=False):
    self.textedit.moveCursor(qtg.QTextCursor.Start)
    if case_sensitive:
        cur = self.textedit.find(
            term,
            qtg.QTextDocument.FindCaseSensitively
        )
    else:
        cur = self.textedit.find(term)
    if not cur:
        self.statusBar().showMessage('No matches Found', 2000)

Now the function is working properly but the textedit.find(term) seems to just lightly highlight the found text like this现在该功能正常工作,但textedit.find(term)似乎只是轻轻地突出显示这样找到的文本

My question is if textedit.find(term) can select the found text like this我的问题是textedit.find(term)可以像这样选择找到的文本

You have to change the color associated with QPalette::Highlight:您必须更改与 QPalette::Highlight 关联的颜色:

p = self.textedit.palette()
p.setColor(qtg.QPalette.Highlight, QColor("blue"))
self.textedit.setPalette(p)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM