Re: QTextEdit and mark Text
You are highlighting the whole line here. Is that what you want? Maybe the content of the text edit is a single line?
Re: QTextEdit and mark Text
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 ...
Re: QTextEdit and mark Text
This works for me:
Code:
#include <QTextEdit>
#include <QApplication>
#include <QTextDocument>
#include <QTextCursor>
int main(int argc, char **argv){
for(int i
=0;i<
20;i
++) str
+=QString("Line%1\n").
arg(i
+1);
doc.setPlainText(str);
te.setDocument(&doc);
do{
cur = doc.find("7", cur);
if(!cur.isNull()){
format.setBackground(Qt::green);
cur.setCharFormat(format);
}
} while(!cur.isNull());
te.show();
return app.exec();
}
Re: QTextEdit and mark Text
Your example works fine. But where is the problem here?
Code:
void QIDTextEdit::highlightText()
{
cursor = this->document()->find("main", cursor);
format.setBackground(Qt::green);
cursor.setCharFormat(format);
}
Nothing happens ... The function is executet normally and "main" is in the text of the document ...
Re: QTextEdit and mark Text
Anyone got an hint for this problem?
Re: QTextEdit and mark Text
Does find() return a valid cursor?
Re: QTextEdit and mark Text
Hmm ... No ... But this->document()->toPlainText(); returns the corrent text - with the word "main" ...
Re: QTextEdit and mark Text
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.
Re: QTextEdit and mark Text
I don't understand what you mean ...
Re: QTextEdit and mark Text
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).
Re: QTextEdit and mark Text
And how can I tell find() not to skip the part?
Re: QTextEdit and mark Text
Pass it an empty cursor or one that is positioned at the beginning of the text.