How to check if the selected word is bold or not
-
Hello, basicaly i want to check if the selected word in textEdit is bold already or not, if it's bold, then to check the bold button, if not, don't do anything. this is the code, what's wrong?
@QTextCursor tc = ui->textEdit->textCursor();
tc.select(QTextCursor::WordUnderCursor);
if(tc.charFormat().fontWeight() == QFont::Bold) {
ui->actionBold->setChecked(true);
}@ -
First connect the QTextEdit::currentCharFormatChanged SIGNAL to a custom SLOT where you will update the actionBold state
@connect(textEdit,SIGNAL(currentCharFormatChanged(QTextCharFormat)),
SLOT(charFormatChanged(QTextCharFormat)));
@where the custom slot is something like this (change MyTextEdit to your class name)
@void MyTextEdit::charFormatChanged(QTextCharFormat cf)
{
ui->actionBold->setChecked(cf.fontWeight() == QFont::Bold )
}@