1.创建QTableView内容
QStandardItemModel *model = new QStandardItemModel(ui->tableView); // 四列 model->setHorizontalHeaderItem(0, new QStandardItem("1")); model->setHorizontalHeaderItem(1, new QStandardItem("Name")); model->setHorizontalHeaderItem(2, new QStandardItem("3")); model->setHorizontalHeaderItem(3, new QStandardItem("4")); // 三行 model->setItem(0, 0, new QStandardItem("001")); model->setItem(0, 1, new QStandardItem("One")); model->setItem(0, 2, new QStandardItem("011")); model->setItem(0, 3, new QStandardItem("111")); model->setItem(1, 0, new QStandardItem("002")); model->setItem(1, 1, new QStandardItem("Two")); model->setItem(1, 2, new QStandardItem("122")); model->setItem(1, 3, new QStandardItem("222")); model->setItem(2, 0, new QStandardItem("003")); model->setItem(2, 1, new QStandardItem("Three")); model->setItem(2, 2, new QStandardItem("133")); model->setItem(2, 3, new QStandardItem("333"));
2.获取根据第几列查询,并补全列表
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(); proxyModel->setSourceModel(model); proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive); proxyModel->setFilterKeyColumn(1); // 第二列,根据名字查找 ui->tableView->setModel(proxyModel);
3.搜索框填写背景内容
ui->lineEditSearch->setPlaceholderText("Search here...");
4.连接信号和槽
QObject::connect(ui->lineEditSearch, &QLineEdit::textChanged, [=](const QString &text) { proxyModel->setFilterRegExp(QString(".*%1.*").arg(text)); });
标签:Qt,setItem,QTableView,搜索,setHorizontalHeaderItem,new,model,QStandardItem,proxyMo From: https://www.cnblogs.com/gnos1s/p/18145199