Important: Please read the Qt Code of Conduct -

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();;
    if(tc.charFormat().fontWeight() == QFont::Bold) {

  • Did you look at the text edit example, I think it has the proper code to do what you ask.

  • yes, i did, but it doesnt have what i need.

  • First connect the QTextEdit::currentCharFormatChanged SIGNAL to a custom SLOT where you will update the actionBold state

    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 )

  • Please notice that bold is not a boolean, its an int (weight). See QFont for more details.

  • Oh, another important detail is that each block (aka paragraph) has a char format too which is applied as a default for the whole paragraph. See the cursor API for blockCharFOrmat

Log in to reply