This works for me:
#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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks