
1、在 Qt 中,默认的编码是 Unicode,书写的代码文件被强制转换为 utf8,但是,在简体中文版的 Windows *** 作系统中,默认编码却是 GBK。
2、因此,在编译 Qt 程序时,如果代码中含有特定中文字符,Qt 的编译器就会发生误判,向我们报告“常量中有换行符”。
3、这时需要打开Qt Creator,点击菜单“工具”-“选项”。
4、在“文本编辑器”-“行为”选项卡中,将文件编码更改为 UTF-8,并且选择“如果编码是UTF-8则添加”。
5、或者,在代码中用 QString.toLocal8Bit( ) 将 Unicode 编码转换为本地系统编码,就完成了。
用QT查找字符串并标记要查找的内容,使用以下代码即可实现:
QString searchString = ui->lineEdit_2->text()QTextDocument *document = ui->description->document()
ui->description->setHtml(ui->description->document()->toPlainText())
int number=0
bool found = false
QTextCursor highlightCursor(document)
QTextCharFormat plainFormat(highlightCursor.charFormat())
QTextCharFormat colorFormat = plainFormat
colorFormat.setForeground(Qt::red)
if(ui->description->toPlainText()==""){
QMessageBox::information(this, tr("description first"),
"Sorry, please display the description first!")
}
else{
ui->result->setPlainText("")
QString resultstring="搜索结果:"
QString laststring
/*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+="n occurrence"+QString::number(number,10)+":—— position:"+QString::number(highlightCursor.position(),10)
}//这个是实现大小写区分效果的代码,你可能不需要
else
resultstring+="n occurrence"+QString::number(number,10)+":—— position:"+QString::number(highlightCursor.position(),10)
}
}
/*关键代码结束*/
if(number>1&&ui->onlyTwo->isChecked()==true)
resultstring+="n occurrence"+QString::number(number,10)+":—— position:"+laststring
ui->result->setPlainText(resultstring)
if (found == false) {
QMessageBox::information(this, tr("Word Not Found"),
"Sorry, the word cannot be found.")
}
else {
QMessageBox::information(this, tr("Word was Found"),
"the word '"+searchString+"' was found for "+QString::number(number,10)+((number>1)? " times":" time"))
}
}
开发环境:win10、QT5.9
开发工具:vs2019
1、根据搜索框的内容匹配下面的索引,找到则显示该字段及该字段的上下索引;
2、当找不到该内容则全都不显示;
3、部分字段匹配搜索,输入的字段与索引项存在部分匹配的时候则同样会显示该索引项及其父子索引项;
1、实现该界面
该界面包括一个QLineEdit控件和一个QTreeWidget控件。
//使用递归的方式向目标节点的父节点进行递归查找
void TableWidgwet::showParent(QTreeWidgetItem* pItem)
{
if (pItem != nullptr) //判断当前节点是否存在
{
QTreeWidgetItem* pTreeParentItem = pItem->parent()//获取当前节点的父节点
if (pTreeParentItem != nullptr) //判断父节点是否存在
{
pTreeParentItem->setHidden(false) //将该父级索引项设置为显示
showParent(pTreeParentItem)//直接传入处理过的节点
//避免传入该节点后递归后跳转到祖父节点
}
}
}
//使用递归的方式遍历子节点同时设置为显示
void TableWidgwet::showSon(QTreeWidgetItem* pItem)
{
int iChild = pItem->childCount() //获取该子节点的数目
for (int i = 0i <iChild++i)//遍历当前项的子节点并设置
{
pItem->child(i)->setHidden(false)
showSon(pItem->child(i))//对当前 子项进行递归
}
}
3、对当前的节点进行遍历查找且对遍历的节点进行显示和隐藏的设置
//依旧是采用递归的方式实现
void TableWidgwet::searchItem(QTreeWidgetItem* tableItem,QString&strText)
{
if (tableItem != nullptr)//防止野指针的问题
{
for (int i = 0i <tableItem->childCount()++i)
{
QTreeWidgetItem* pTreeItem = tableItem->child(i)
if (pTreeItem != nullptr)
{
//如何索引项字段部分匹配于输入框字段则设置该项及其父子项显示且展开
if (pTreeItem->text(0).contains(strText))
{
pTreeItem->setHidden(false)
pTreeItem->setExpanded(true);
showSon(pTreeItem)
showParent(pTreeItem)
}
else
{
pTreeItem->setHidden(true)//不匹配则隐藏
searchItem(pTreeItem, strText)//递归遍历
}
}
}
}
}
4、对整个树形索引控件的一级索引项进行遍历,并调用上述的函数实现对每个一级索引项下的索引项进行递归遍历设置
void TableWidgwet::showItem()
{
for (int i = 0i <ui.treeWidget->topLevelItemCount()++i)
{
if (ui.treeWidget->topLevelItem(i)->text(0).contains(ui.lineEdit->text()))
{
ui.treeWidget->topLevelItem(i)->setHidden(false)
}
else
{
ui.treeWidget->topLevelItem(i)->setHidden(true)
}
searchItem(ui.treeWidget->topLevelItem(i), ui.lineEdit->text())
}
}
5、使用connect对QLineEdit的textchange事件响应与槽函数绑定处理
connect(ui.lineEdit, &QLineEdit::textChanged, this, &TableWidgwet::showItem)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)