QTextEdit* textEdit = new QTextEdit(); QTextCursor cursor = textEdit->textCursor(); QFont font("Arial"); font.setBold(true); QTextCharFormat format; format.setFont(font); format.setForeground(Qt::red); cursor.select(QTextCursor::WordUnderCursor); cursor.setCharFormat(format);
QTextEdit* textEdit = new QTextEdit(); QTextCursor cursor = textEdit->textCursor(); QTextCharFormat format = cursor.charFormat(); format.setBackground(Qt::yellow); cursor.setCharFormat(format);In this example, the current QTextCursor's text formatting is retrieved and the background color is set to yellow. Overall, QTextCursor setCharFormat function is useful for easily applying formatting changes to text in a QTextEdit widget.