(PyQt) 如何重置整个 QTextEdit 的 CharFormat?
(PyQt) How can I reset the CharFormat of an entire QTextEdit?
我在我的 QTextEdit 中对几个单词使用 mergeCharFormat,以突出显示它们。像这样:
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 的字符格式设置回默认值?
到目前为止我已经尝试过再次获取光标,并将其格式设置为具有清晰背景的新格式,然后将光标放在整个选择上并使用 QTextCursor.setCharFormat(),但是这似乎什么都不做。
对整个文档应用新的 QTextCharFormat
适合我:
def drawGUI():
...
cursor.mergeCharFormat(format_)
def clear():
cursor = editBox.textCursor()
cursor.select(QtGui.QTextCursor.Document)
cursor.setCharFormat(QtGui.QTextCharFormat())
cursor.clearSelection()
editBox.setTextCursor(cursor)
button = QtGui.QPushButton('Clear')
button.clicked.connect(clear)
layout = QtGui.QVBoxLayout(w)
layout.addWidget(editBox)
layout.addWidget(button)
我在我的 QTextEdit 中对几个单词使用 mergeCharFormat,以突出显示它们。像这样:
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 的字符格式设置回默认值?
到目前为止我已经尝试过再次获取光标,并将其格式设置为具有清晰背景的新格式,然后将光标放在整个选择上并使用 QTextCursor.setCharFormat(),但是这似乎什么都不做。
对整个文档应用新的 QTextCharFormat
适合我:
def drawGUI():
...
cursor.mergeCharFormat(format_)
def clear():
cursor = editBox.textCursor()
cursor.select(QtGui.QTextCursor.Document)
cursor.setCharFormat(QtGui.QTextCharFormat())
cursor.clearSelection()
editBox.setTextCursor(cursor)
button = QtGui.QPushButton('Clear')
button.clicked.connect(clear)
layout = QtGui.QVBoxLayout(w)
layout.addWidget(editBox)
layout.addWidget(button)