tableWidget搜索筛选功能
本想直接在CSDN上搜现成的按关键字搜索功能,无奈没找到合适的,只能自己实现一把,没找到合适的同学可以参考一下
下面代码片段可直接全部放入搜索按钮的槽函数
// ui->sousuo->text() 为lineEdit搜索框内的输入内容 下面的同理
if (ui->sousuo->text() == "")
{
for (int i =0; i<20;i++) // 这里的20代表tableWidget的总行数,可根据自己情况换掉
{
ui->tableWidget->setRowHidden(i, false); // 显示第i行
}
return;
}
int b[20] = {0}; // 命名比较随意
int tm =0; // 命名比较随意
for (int i = 0 ;i <20; i ++) // 这里的20代表tableWidget的总行数,可根据自己情况换掉
{
int count =0;
for (int j = 0; j<5;j++) // 这里的5代表tableWidget的总列数,可根据自己情况换掉
{
if (ui->sousuo->text() == ui->tableWidget->item(i,j)->text())
{
count++;
continue;
}
}
if (count == 0)
{
b[tm]= i;
tm++;
}
}
for (int i =0; i<tm;i++)
{
ui->tableWidget->setRowHidden(b[i], true); // 隐藏第i行
}
实现效果口头描述一下: