PDA

View Full Version : QTextEdit and mark Text



J-jayz-Z
27th September 2007, 11:00
Hi,

I want to code a search, like the text search in FireFox.
And I've got a problem with highlighting the text. I know that the background color can be changed with QTextCharFormat::setBackground(QBrush) - but how can I do that with a part of the document and not with the whole?

My Idea was like

QTextCursor cursor = this->textCursor();
cursor.movePosition(QTextCursor::StartOfLine);
cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
QTextCharFormat format = cursor.charFormat();
format.setBackground(QBrush(Qt::green));
cursor.setCharFormat(format);
But nothing happens. When putting the format into the QTextEdit, the whole document has the background color green.

The next problem should be, finding the Textposition in the document. But first this one ...

EDIT: I forgot to call QTextEdit::setTextCursor ... After calling this, the document is completly green in the background ...

wysota
27th September 2007, 11:08
You are highlighting the whole line here. Is that what you want? Maybe the content of the text edit is a single line?

J-jayz-Z
27th September 2007, 11:19
No, it has more than 20 lines in the test. I want to highlight one line, but all lines are highlighted here, that is my problem ... And I've got no idea, why ...

wysota
27th September 2007, 11:35
This works for me:

#include <QTextEdit>
#include <QApplication>
#include <QTextDocument>
#include <QTextCursor>



int main(int argc, char **argv){
QApplication app(argc, argv);
QTextDocument doc;
QString str;
for(int i=0;i<20;i++) str+=QString("Line%1\n").arg(i+1);
doc.setPlainText(str);
QTextEdit te;
te.setDocument(&doc);
QTextCursor cur = te.textCursor();
do{
cur = doc.find("7", cur);
if(!cur.isNull()){
cur.select(QTextCursor::LineUnderCursor);
QTextCharFormat format = cur.charFormat();
format.setBackground(Qt::green);
cur.setCharFormat(format);
}
} while(!cur.isNull());
te.show();
return app.exec();
}

J-jayz-Z
27th September 2007, 13:03
Your example works fine. But where is the problem here?

void QIDTextEdit::highlightText()
{
QTextCursor cursor = this->textCursor();
cursor = this->document()->find("main", cursor);
cursor.select(QTextCursor::LineUnderCursor);
QTextCharFormat format = cursor.charFormat();
format.setBackground(Qt::green);
cursor.setCharFormat(format);
}
Nothing happens ... The function is executet normally and "main" is in the text of the document ...

J-jayz-Z
27th September 2007, 14:28
Anyone got an hint for this problem?

wysota
27th September 2007, 15:14
Does find() return a valid cursor?

J-jayz-Z
27th September 2007, 15:23
Hmm ... No ... But this->document()->toPlainText(); returns the corrent text - with the word "main" ...

wysota
27th September 2007, 15:39
So either the text is incorrect despite what you may think or the initial cursor position is after the last occurance of the term you seek.

J-jayz-Z
27th September 2007, 16:15
I don't understand what you mean ...

wysota
27th September 2007, 16:44
find() can fail (return a null cursor) in two cases - either the searched term doesn't exist in the text or it exists but you told find() to skip the part of text where the searched term exists (that's what the cursor argument to find() is for).

J-jayz-Z
27th September 2007, 22:10
And how can I tell find() not to skip the part?

wysota
28th September 2007, 00:14
Pass it an empty cursor or one that is positioned at the beginning of the text.