Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to change color of words by clicking the words in QPlainTextEdit
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • kybernetesK Offline
    kybernetesK Offline
    kybernetes
    wrote on last edited by
    #1

    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.

    3/75
    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      hi
      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();
      
          cursor.select(QTextCursor::WordUnderCursor);
          QString theText=cursor.selectedText();
          // add theText to list for later use.
          QTextCharFormat format;
          format.setFontWeight(  QFont::Bold  );
          if (cursor.hasSelection()) {
            cursor.setCharFormat( format );
            ui->plainTextEdit->setTextCursor(cursor);
          }
      }
      
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved