How to change color of words by clicking the words in QPlainTextEdit

  • Hello,

    By clicking on the word in a plainTextEditor I want to change its color and count the changed words.

    For example, as in the following text, i want to show 3 clicked words.

    Aliquid quasar duo Scaevola, et zril lobortis sea. Id modus commune persequeris cum. Disputando liberaviss e quoi, duo affert singulis, repudiar to quaeri thought situ ta. Errea I quaequ to conclusi onemqu et eam, I vocent goods to the pro delenit the expetendis reprimiqu at the menu. Van offendit pertinaci what I'm queer, meat albucius dissentias vixen. Utina I Munera officiis pri c, quis I repudiar in pri goods, debet concepta I dissentiet ex usu.

    Clicked words count / total number of words in the text

    after that i will change the clicked words automaticly from a library which i generated (like autocorrect)
    but this is the second phase of my project

    So can you give me any advise how to do that?

  • Lifetime Qt Champion

    from a button click, when cursor over a word.
    Do not call it from "selection changed" of PlainText
    as it seems to call multiple times and cause the code to crash.
    Not sure why. Did not investigate. Maybe you can fix it :)

    void MainWindow::on_pushButton_clicked() {
        QTextCursor cursor = ui->plainTextEdit->textCursor();
        QString theText=cursor.selectedText();
        // add theText to list for later use.
        QTextCharFormat format;
        format.setFontWeight(  QFont::Bold  );
        if (cursor.hasSelection()) {
          cursor.setCharFormat( format );

Log in to reply