需求
qt 中需要从数据库中搜索数据,并在表格中显示
解决
设置
view_ = new QTableView(this);
query_model_ = new QSqlQueryModel(this);
selection_model_ = new QItemSelectionModel(query_model_, this);
view_->setModel(query_model_);
view_->setSelectionModel(selection_model_);
view_->setEditTriggers(QAbstractItemView::NoEditTriggers);
view_->setSelectionMode(QAbstractItemView::SingleSelection);
view_->setSelectionBehavior(QAbstractItemView::SelectRows);
view_->setAlternatingRowColors(true);
搜索并显示
QString query_command = "SELECT q.`flag_check`, q.`strip_id`, q.`analysis_datetime`, q.`ip`, q.`analysis_id`, qt.`name`, "
"r.`type`, q.`value_fix`, q.`value_act_fix`, q.`value_cr_fix`, "
"q.`reference_range`, q.`quality_no`, "
"(CASE WHEN q.`flag_normal` = 0 THEN '不通过' "
"WHEN q.`flag_normal` = 1 THEN '通过' "
"ELSE '不通过' END), q.`device_sn` "
"FROM (quality AS q inner join quality_type AS qt ON q.`fk_quality_type__quality` = qt.`key_id`) "
"inner join reagent AS r ON q.`fk_reagent__quality` = r.`key_id` "
"WHERE q.`key_id` > 0";
view_->hideColumn(13);
QSqlRecord rec = query_model_->record();
query_model_->setHeaderData(rec.indexOf("flag_check"), Qt::Horizontal, "审核");
query_model_->setHeaderData(rec.indexOf("strip_id"), Qt::Horizontal, "样本号");
query_model_->setHeaderData(rec.indexOf("analysis_datetime"), Qt::Horizontal, "检测日期");
query_model_->setHeaderData(rec.indexOf("ip"), Qt::Horizontal, "仪器IP");
query_model_->setHeaderData(rec.indexOf("analysis_id"), Qt::Horizontal, "检测编号");
query_model_->setHeaderData(rec.indexOf("name"), Qt::Horizontal, "样本类型");
query_model_->setHeaderData(rec.indexOf("type"), Qt::Horizontal, "试剂类型");
query_model_->setHeaderData(rec.indexOf("value_fix"), Qt::Horizontal, "结果");
query_model_->setHeaderData(rec.indexOf("value_act_fix"), Qt::Horizontal, "ACT");
query_model_->setHeaderData(rec.indexOf("value_cr_fix"), Qt::Horizontal, "CR");
query_model_->setHeaderData(rec.indexOf("reference_range"), Qt::Horizontal, "参考范围");
query_model_->setHeaderData(rec.indexOf("quality_no"), Qt::Horizontal, "质控品号");
query_model_->setHeaderData(rec.indexOf("(CASE WHEN q.`flag_normal` = 0 THEN '不通过' "
"WHEN q.`flag_normal` = 1 THEN '通过' "
"ELSE '不通过' END)"), Qt::Horizontal, "结论");