QT 使用 QTableView 和 QSqlQueryModel 配合显示数据
需求 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....