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 select the highlighted string in QPlaintTextEdit

How to select the highlighted string in QPlaintTextEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 495 Views 1 Watching
  • 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.
  • AmrCoderA Offline
    AmrCoderA Offline
    AmrCoder
    wrote on last edited by
    #1

    I have a QPlainTextEdit which have highlighted strings is there any way to get this text when I put the cursor on it I try something like

      QTextCursor cur(ui->plainTextEdit_PREVIEW->document());
      cur.select(QTextCursor::BlockUnderCursor); // to get all the line
      if (!cur.selectedText().isEmpty()) {
      qDebug() << "block = " << cur.selectedText();
      }
    

    I try also QTextCursor::WordUnderCusor but all not work because may the highlighted string consists of many words is there to so in Qt or i should to code it
    I try to loop through the text left and right the cusor and check if it's background is colored or not and it works I ask if there is a better way to to this

    A 1 Reply Last reply
    0
    • AmrCoderA AmrCoder

      I have a QPlainTextEdit which have highlighted strings is there any way to get this text when I put the cursor on it I try something like

        QTextCursor cur(ui->plainTextEdit_PREVIEW->document());
        cur.select(QTextCursor::BlockUnderCursor); // to get all the line
        if (!cur.selectedText().isEmpty()) {
        qDebug() << "block = " << cur.selectedText();
        }
      

      I try also QTextCursor::WordUnderCusor but all not work because may the highlighted string consists of many words is there to so in Qt or i should to code it
      I try to loop through the text left and right the cusor and check if it's background is colored or not and it works I ask if there is a better way to to this

      A Offline
      A Offline
      ambershark
      wrote on last edited by ambershark
      #2

      @AmrCoder You're using the wrong text cursor. The document and textedit cursor's are different.

      Here is the code you want:

      qDebug() << ui->plainTextEdit->textCursor().selectedText();
      

      Edit: I may be misunderstanding this... when you say highlighted strings, are you talking about the highlighted SELECTED string, or some other type of highlighting? My code above works on normal highlighted selections but not if you are doing something custom or talking about highlighting in the sense of colorization.

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      3

      • Login

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