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:
@
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
-
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);
}
}
@ -
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. -
@
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.
-
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.