November 29, 2011

haney haney
Lab Rat
36 posts

Need help on QTextEdit

 

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:

  1. void MainWindow::replaceText()
  2. {
  3.     textEdit->moveCursor(QTextCursor::Start);
  4.  
  5.     if(textEdit->find(findLineEdit->text(), QTextDocument::FindCaseSensitively))
  6.     {
  7.        textEdit->insertPlainText(replaceLineEdit->text());
  8.        textEdit->textCursor().select(QTextCursor::WordUnderCursor); // here text is not getting selected
  9.     }
  10.  
  11. }

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

8 replies

November 29, 2011

Volker Volker
Ant Farmer
5428 posts

To cite QTextEdit::textCursor() [doc.qt.nokia.com] API docs:

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:

  1. void MainWindow::replaceText()
  2. {
  3.     textEdit->moveCursor(QTextCursor::Start);
  4.  
  5.     if(textEdit->find(findLineEdit->text(), QTextDocument::FindCaseSensitively))
  6.     {
  7.        textEdit->insertPlainText(replaceLineEdit->text());
  8.        QTextCursor c = textEdit->textCursor();
  9.        c.select(QTextCursor::WordUnderCursor);
  10.        textEdit->setTextCursor(c);
  11.     }
  12. }

November 29, 2011

haney haney
Lab Rat
36 posts

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.

November 29, 2011

haney haney
Lab Rat
36 posts

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.

November 29, 2011

Andre Andre
Robot Herder
6395 posts

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

November 29, 2011

haney haney
Lab Rat
36 posts

  1. if(textEdit->find(findLineEdit->text(), QTextDocument::FindCaseSensitively))
  2.     {
  3.        textEdit->insertPlainText(replaceLineEdit->text());
  4.        textEdit->moveCursor(QTextCursor::StartOfWord);
  5.        QTextCursor c = textEdit->textCursor();
  6.        c.select(QTextCursor::WordUnderCursor);
  7.  
  8.        QTextCharFormat format = c.charFormat();
  9.        format.setBackground(Qt::green); // this will set the background of word to green
  10.        c.setCharFormat(format);
  11.  
  12.       }

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.

November 29, 2011

Volker Volker
Ant Farmer
5428 posts

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

December 1, 2011

haney haney
Lab Rat
36 posts

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

  1. QList<QTextEdit::ExtraSelection> extraSelections;
  2. if(!textEdit->isReadOnly())
  3.  {
  4.  QColor color = QColor(Qt::red).lighter(130);
  5.  textEdit->find(findLineEdit->text(), QTextDocument::FindCaseSensitively);
  6.  QTextEdit::ExtraSelection extra;
  7.  extra.format.setBackground(color);
  8.  extra.cursor = textEdit->textCursor();
  9.  extraSelections.append(extra);
  10.  }
  11.  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.

December 1, 2011

Volker Volker
Ant Farmer
5428 posts

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.

 
  ‹‹ what is qt equalient of normal win32 window      [Solved] Catch application crash and/or display message ››

You must log in to post a reply. Not a member yet? Register here!