600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > qstring 字符串查找_怎样用QT查找字符串并标记要查找的内容

qstring 字符串查找_怎样用QT查找字符串并标记要查找的内容

时间:2020-02-28 15:47:31

相关推荐

qstring 字符串查找_怎样用QT查找字符串并标记要查找的内容

展开全部

用QT查找字符串并标记要查找的内容32313133353236313431303231363533e78988e69d8331333363373762,使用以下代码即可实现:QStringsearchString=ui->lineEdit_2->text();

QTextDocument*document=ui->description->document();

ui->description->setHtml(ui->description->document()->toPlainText());

intnumber=0;

boolfound=false;

QTextCursorhighlightCursor(document);

QTextCharFormatplainFormat(highlightCursor.charFormat());

QTextCharFormatcolorFormat=plainFormat;

colorFormat.setForeground(Qt::red);

if(ui->description->toPlainText()==""){

QMessageBox::information(this,tr("descriptionfirst"),

"Sorry,pleasedisplaythedescriptionfirst!");

}

else{

ui->result->setPlainText("");

QStringresultstring="搜索结果:";

QStringlaststring;

/*while循环体是本代码的关键-——开始——*/

while(!highlightCursor.isNull()&&!highlightCursor.atEnd()){

if(ui->daxiaocheckBox->isChecked()==true){

highlightCursor=document->find(searchString,highlightCursor,QTextDocument::FindCaseSensitively);

}//这个是实现大小写区分效果的代码,你可能不需要

else

highlightCursor=document->find(searchString,highlightCursor);

if(!highlightCursor.isNull()){

number++;

found=true;

highlightCursor.movePosition(QTextCursor::Right,QTextCursor::KeepAnchor,0);

highlightCursor.mergeCharFormat(colorFormat);

laststring=QString::number(highlightCursor.position(),10);

if(ui->onlyTwo->isChecked()==true){

if(number<=1)

resultstring+="noccurrence"+QString::number(number,10)+":——position:"+QString::number(highlightCursor.position(),10);

}//这个是实现大小写区分效果的代码,你可能不需要

else

resultstring+="noccurrence"+QString::number(number,10)+":——position:"+QString::number(highlightCursor.position(),10);

}

}

/*关键代码结束*/

if(number>1&&ui->onlyTwo->isChecked()==true)

resultstring+="noccurrence"+QString::number(number,10)+":——position:"+laststring;

ui->result->setPlainText(resultstring);

if(found==false){

QMessageBox::information(this,tr("WordNotFound"),

"Sorry,thewordcannotbefound.");

}

else{

QMessageBox::information(this,tr("WordwasFound"),

"theword'"+searchString+"'wasfoundfor"+QString::number(number,10)+((number>1)?"times":"time"));

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。