I want to format the selected text in QTextEdit by clicking a button. For axample, I want to make it bold if it is not bold, or non-bold if it is in bold. Please help me with an example.
EDIT:
In fact, I already found the qt demo code for a text editor that does what I need:
void
MyTextEdit::boldText(bool isBold)
{
QTextCharFormat fmt;
fmt.setFontWeight(isBold ? QFont::Bold : QFont::Normal);
mergeFormatOnWordOrSelection(fmt);
}
void
MyTextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
{
QTextCursor cursor = m_textEdit->textCursor();
if (!cursor.hasSelection())
cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(format);
m_textEdit->mergeCurrentCharFormat(format);
}
But I canβt understand what the textCursor () method returns and how the property merge is done? Just some formats change, some of them remain constant. As a function, mergeCharFormat understands what to change and what to leave as is. Please explain to me only these two things. Thank.
Narek