Important: Please read the Qt Code of Conduct -

QTextEdit Check if selected section is 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;
    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 has the following span. I have to break it up cause when i post the html it gets removed:

    font-weight:600; color:#313131;
    Data between the span: functions

    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?

  • Anyone ? Im sure someone has attempted this or ran into this before. I guess I want to know if their is an qt api or if I would need to parse it myself. Really its more than checking if its bold. It can be if its italic or underline, etc. I have a strong feeling that I will have to parse it myself. But before I go that route I want to make sure their is no qt api

  • I see two ways to do it

    Keep track of all text pieces like letters, words, sentences, etc. That way you will know everything and you will be able to find the changes quickly. But it will take some memory.

    Don't track and find all changes on a fly like you described. Less memory usage but more time to process.

    Also you can combine both methods.

  • @andreyc thanks for the feedback. I'm thinking that the second option may make the best sense. Thanks for the tip

  • You are welcome.
    You may consider "QXml....": classes to keep your document. Maybe it will make easier to search.

    [EDIT]: Scratch my suggestion about QXml... I did not realize that you using QText... class.

Log in to reply