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. Need help on QTextEdit
Forum Updated to NodeBB v4.3 + New Features

Need help on QTextEdit

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 7.6k 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.
  • H Offline
    H Offline
    haney
    wrote on last edited by
    #1

    Hi all,

    In my texteditor, I added find&replace option. The logic is working fine but I need to highlight ( or select ) the text that is found or replaced. It will be good if text found or getting replaced has some background color or highlighted.

    Code:
    @
    void MainWindow::replaceText()
    {
    textEdit->moveCursor(QTextCursor::Start);

    if(textEdit->find(findLineEdit->text(), QTextDocument::FindCaseSensitively))
    {
       textEdit->insertPlainText(replaceLineEdit->text());
       textEdit->textCursor().select(QTextCursor::WordUnderCursor); // here text is not getting selected
    }
    

    }
    @
    Plz help me regarding this..

    Thanks,
    Haney.

    Edit: Please use @ tags around code sections. There is a button for it at the top of the editor too; Andre

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      To cite "QTextEdit::textCursor() ":http://doc.qt.nokia.com/4.7/qtextedit.html#textCursor API docs:

      bq. Returns a copy of the QTextCursor that represents the currently visible cursor. Note that changes on the returned cursor do not affect QTextEdit's cursor; use setTextCursor() to update the visible cursor.

      Change your code to:

      @
      void MainWindow::replaceText()
      {
          textEdit->moveCursor(QTextCursor::Start);
       
          if(textEdit->find(findLineEdit->text(), QTextDocument::FindCaseSensitively))
          {
             textEdit->insertPlainText(replaceLineEdit->text());
             QTextCursor c = textEdit->textCursor();
      c.select(QTextCursor::WordUnderCursor);
      textEdit->setTextCursor(c);
          }
      }
      @

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • H Offline
        H Offline
        haney
        wrote on last edited by
        #3

        Hi,

        Thanks for the reply..

        I changed the code and it is working for only the first time the word is replaced. Next time when I press replace button again it is replacing the word but that word is not visible. Third time when I press replace button third occurrence is not visible but now second occurrence is visible and it goes on till last occurrence of the word.

        Plz help me if can get the word selected every time when it is replaced.

        Thanks,
        Haney.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          haney
          wrote on last edited by
          #4

          Instead of selecting the word that is found or replaced, it would be good if we can set any background color for the word which is found or replaced.
          this is just my idea still working on it.

          Appreaciate ur help in this regard.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Perhaps you can (ab)use [[doc:QSyntaxHighlighter]] in some way? Just an idea, I never tried this myself. If not, it might still provide the inspiration you need.

            1 Reply Last reply
            0
            • H Offline
              H Offline
              haney
              wrote on last edited by
              #6

              @
              if(textEdit->find(findLineEdit->text(), QTextDocument::FindCaseSensitively))
              {
              textEdit->insertPlainText(replaceLineEdit->text());
              textEdit->moveCursor(QTextCursor::StartOfWord);
              QTextCursor c = textEdit->textCursor();
              c.select(QTextCursor::WordUnderCursor);

                 QTextCharFormat format = c.charFormat();
                 format.setBackground(Qt::green); // this will set the background of word to green
                 c.setCharFormat(format);
              
                }
              

              @

              I tried this to set some background color in order to highlight the replaced word. But after textEdit gains back focus the highlighted part should come to normal and that is not happening.. I tried clearBackground() and it dint work for me..

              Please share any other ideas to highlight the replaced word similar to implementation of find/replace option in QtCreator IDE.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                QTextEdit::ExtraSelection could be a solution for you. Have a look at my wiki article on "spell checking with hunspell":/wiki/Spell_Checking_with_Hunspell, it shows you how to use it.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  haney
                  wrote on last edited by
                  #8

                  Thanks for ur reply.. I tried the following code in my findNext() slot but it did not work.

                  @
                  QListQTextEdit::ExtraSelection extraSelections;
                  if(!textEdit->isReadOnly())
                  {
                  QColor color = QColor(Qt::red).lighter(130);
                  textEdit->find(findLineEdit->text(), QTextDocument::FindCaseSensitively);
                  QTextEdit::ExtraSelection extra;
                  extra.format.setBackground(color);
                  extra.cursor = textEdit->textCursor();
                  extraSelections.append(extra);
                  }
                  textEdit->setExtraSelections(extraSelections);
                  @

                  Can you plz share a piece of code that sets some background color of word and when I close my find/replace dialog that color should disappear.

                  Thanks,
                  Haney.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    That code is given in the wiki page too. If you search a little bit you find a comment "reset the word highlight". You need to reset the extra highlight.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    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