/*
查找文本信息
注意这里是功能按键,所发信号属于QAction类
*/
connect(paction_find, &QAction::triggered, this, [=](){
QDialog *dia = new QDialog(this);
dia->setWindowTitle("查找文本信息");
dia->resize(160,90);
dia->setModal(true);
//标签、文本框、按钮,都创建在对话框里面
QLabel *label = new QLabel(tr("点击查找"),dia);
label->move(22,5);
textLineEdit = new QLineEdit(dia);
textLineEdit->setFixedSize(60,38);
textLineEdit->setStyleSheet ("border:2px groove gray;border-radius:10px;padding:2px 4px;" "color:rgb(110,174,248)");
textLineEdit->move(20,20);
btn = new QPushButton(dia);
btn->setFixedSize(60,38);
btn->setStyleSheet ("border:2px groove gray;border-radius:10px;padding:2px 4px;" "color:rgb(110,174,248)");
btn->setText("搜索文本");
btn->move(80,20);
dia->show();
connect(btn, &QPushButton::clicked, this, [=](){
QString str = textLineEdit->text();
qDebug() << "QString str = " << str << endl;
bool ok = ptextedit->find(str, QTextDocument::FindWholeWords);//FindWholeWords全词匹配
/*
查找到的文本设置成高亮
QPalette是Qt中的一个调色板类,用于美化界面。
设置的方法是先调用QWidget::palette()获取当前面板,修改它为自定义的值后再
通过方法QWidget::setPalette设置为新修改的面板
*/
QPalette palette = ptextedit->palette();
/*
Highlight:高亮背景色
控件的颜色状态分三种:(1)Active,激活状态 (2)Disabled,禁用状态 (3)Inactive,未激活状态
*/
palette.setColor(QPalette::Highlight, palette.color(QPalette::Active,QPalette::Highlight));
ptextedit->setPalette(palette);
if(!ok)
{
QMessageBox::information(this, tr("查找"), tr("找不到%1").arg(str));
}
else
QMessageBox::information(this, tr("查找"), tr("找到").arg(str));
});
});
200_QT_自行构建一个QDialog对话框,完成内容的点击搜索
最新推荐文章于 2024-03-05 09:41:44 发布