0
私はQTextEdit内のいくつかの単語にmergeCharFormatを使用して強調表示しています。これは次のようなものです。(PyQt)QTextEdit全体のCharFormatをリセットするにはどうすればよいですか?
import sys
from PyQt4 import QtGui, uic
from PyQt4.QtCore import *
def drawGUI():
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
w.setGeometry(200, 200, 200, 50)
editBox = QtGui.QTextEdit(w)
text = 'Hello stack overflow, this is a test and tish is a misspelled word'
editBox.setText(text)
""" Now there'd be a function that finds misspelled words """
# Highlight misspelled words
misspelledWord = 'tish'
cursor = editBox.textCursor()
format_ = QtGui.QTextCharFormat()
format_.setBackground(QtGui.QBrush(QtGui.QColor("pink")))
pattern = "\\b" + misspelledWord + "\\b"
regex = QRegExp(pattern)
index = regex.indexIn(editBox.toPlainText(), 0)
cursor.setPosition(index)
cursor.movePosition(QtGui.QTextCursor.EndOfWord, 1)
cursor.mergeCharFormat(format_)
w.showFullScreen()
sys.exit(app.exec_())
if __name__ == '__main__':
drawGUI()
この強調表示機能は、意図した通りに機能します。しかし、私はテキストエリアからハイライトをクリアする良い方法を見つけることができません。 QTextEdit全体のcharフォーマットをデフォルトに戻すだけで、このようなことを行う良い方法は何ですか?
これまで私が試したことは、カーソルをもう一度取得して、その背景をクリアな背景の新しいフォーマットに設定してから、カーソル全体を選択してQTextCursor.setCharFormat()を使用することです。何もしない。文書全体に新しいQTextCharFormat
を適用
すごいです!私はカーソルのためのちょうど.select()があることを知らなかった、何とかドキュメントでそれを逃した。 – Ajv2324