I want to append a sentence in the QTextEdit of QWidget, such as "I have two books." The word "two" will be blue and bold. And there will be eight spaces between "two" and "books". But I don't know how to do it. Here is an example.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QColor, QFont, QTextCharFormat
from PyQt5 import QtCore
from PyQt5.QtCore import Qt, pyqtSignal
class App(QWidget):
update_message = QtCore.pyqtSignal(str)
def __init__(self):
super().__init__()
self.initUI()
self.update_message.connect(self.updateText)
def initUI(self):
self.setWindowTitle("ZM")
vlayout =QVBoxLayout()
self.bt4 = QPushButton("test command")
self.textEdit = QTextEdit()
self.textEdit.setStyleSheet("background:lightgray; font-family:Helvetica; color:green; font: 18px; white-space:pre")
self.textEdit.setReadOnly(True)
self.textEdit.document().setMaximumBlockCount(100)
self.textEdit.setText('hello')
vlayout.addWidget(self.textEdit)
vlayout.addWidget(self.bt4)
self.setLayout(vlayout)
self.bt4.clicked.connect(self.test_command)
self.resize(1000,800)
self.show()
def updateText (self, text):
self.textEdit.append(text)
def test_command(self):
command = "I have <pre style='color:blue'><b> two </b> books.</pre>"
self.update_message.emit(command)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())