adviner
adviner

Reputation: 3567

QTextEdit check if its bold

I have a bold button that when selected can set the text and make it bold as follows:

QTextCursor cursor = this->ui->editNotes->textCursor();
QTextCharFormat format;
format.setFontWeight(QFont::Bold);
cursor.mergeCharFormat(format);

The question is how to press the same bold button and check if the selected text is bold? So if its already bold i will remove the bold from the same selection. Do I have to parse the html code? Example if I look at the text that I made bold the html data is:

<span style=" font-family:'Arial,Helvetica'; font-size:14px; font-weight:600;    color:#313131;">functions</span>

Is there an easier method then parsing the attributes the text is wrapped in to check the font-weight? Or is this the only solution?

Upvotes: 0

Views: 1381

Answers (1)

Jablonski
Jablonski

Reputation: 18524

It is very old question, but I joined to this site a month ago, so I couldn't answer earlier. So solution is to use font() and bold() methods:

QTextCursor cursor = ui->textEdit_2->textCursor();
QTextCharFormat format;
if(!cursor.charFormat().font().bold())
{
    qDebug() << "not bold";
}
else
{
    qDebug() << "bold";
}

Upvotes: 1

Related Questions