Qt 数据库组件与TableView组件实现联动,以下案例中实现了,当用户点击并选中TableView组件内的某一行时,我们通过该行中的name字段查询并将查询结果关联到ListView
组件内,同时将TableView中选中行的字段分别显示在窗体底部的LineEdit
编辑内,该案例具体实现细节如下。
首先在UI界面中绘制好需要的控件,左侧放一个TableView
组件,右侧是一个ListView
组件,底部放三个LineEdit
组件,界面如下:
我们还是需要创建两张表结构,表Student
用于存储学生的基本信息,表StudentTimetable
存储的是每个学生所需要学习的课程列表,执行后创建数据表。
void InitMultipleSQL () { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE" ); db.setDatabaseName("./lyshark.db" ); if (!db.open()) { std ::cout << db.lastError().text().toStdString()<< std ::endl ; return ; } db.exec("DROP TABLE Student" ); db.exec("CREATE TABLE Student (" "id INTEGER PRIMARY KEY AUTOINCREMENT, " "name VARCHAR(40) NOT NULL, " "age INTEGER NOT NULL)" ); QStringList name_list; name_list << "lyshark" << "lisi" << "wangwu" ; QStringList age_list; age_list << "25" << "34" << "45" ; QSqlQuery query; query.prepare("INSERT INTO Student(name,age) " "VALUES (:name, :age)" ); if (name_list.size() == age_list.size()) { for (int x=0 ;x< name_list.size();x++) { query.bindValue(":name" ,name_list[x]); query.bindValue(":age" ,age_list[x]); query.exec(); } } db.exec("DROP TABLE StudentTimetable" ); db.exec("CREATE TABLE StudentTimetable(" "id INTEGER PRIMARY KEY AUTOINCREMENT, " "name VARCHAR(40) NOT NULL, " "timetable VARCHAR(128) NOT NULL" ")" ); db.exec("INSERT INTO StudentTimetable(name,timetable) VALUES ('lyshark','AAA')" ); db.exec("INSERT INTO StudentTimetable(name,timetable) VALUES ('lyshark','BBB')" ); db.exec("INSERT INTO StudentTimetable(name,timetable) VALUES ('lyshark','CCC')" ); db.exec("INSERT INTO StudentTimetable(name,timetable) VALUES ('lisi','QQQ')" ); db.exec("INSERT INTO StudentTimetable(name,timetable) VALUES ('lisi','WWW')" ); db.exec("INSERT INTO StudentTimetable(name,timetable) VALUES ('wangwu','EEE')" ); db.commit(); db.close(); }
程序运行后,构造函数MainWindow::MainWindow(QWidget *parent)
内初始化表格,查询Student
表内记录,将查询到的指针绑定到theSelection
模型上,绑定后再将绑定指针加入到dataMapper
组件映射中,即可实现初始化,其初始化代码如下:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QSqlDatabase> #include <QSqlError> #include <QSqlQuery> #include <QSqlRecord> #include <iostream> #include <QStringList> #include <QString> #include <QVariant> #include <QDataWidgetMapper> #include <QtSql> #include <QStandardItem> #include <QStringList> #include <QStringListModel> QSqlQueryModel *qryModel; QItemSelectionModel *theSelection; QDataWidgetMapper *dataMapper; MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow) { ui->setupUi(this); QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE" ); db.setDatabaseName("./lyshark.db" ); if (!db.open()) { std ::cout << db.lastError().text().toStdString()<< std ::endl ; return ; } qryModel=new QSqlQueryModel(this); qryModel->setQuery("SELECT * FROM Student ORDER BY id" ); if (qryModel->lastError().isValid()) { return ; } qryModel->setHeaderData(0 ,Qt::Horizontal,"ID" ); qryModel->setHeaderData(1 ,Qt::Horizontal,"Name" ); qryModel->setHeaderData(2 ,Qt::Horizontal,"Age" ); theSelection=new QItemSelectionModel(qryModel); ui->tableView->setModel(qryModel); ui->tableView->setSelectionModel(theSelection); ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows); dataMapper= new QDataWidgetMapper(); dataMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit); dataMapper->setModel(qryModel); dataMapper->addMapping(ui->lineEdit_id,0 ); dataMapper->addMapping(ui->lineEdit_name,1 ); dataMapper->addMapping(ui->lineEdit_age,2 ); dataMapper->toFirst(); connect(theSelection,SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),this,SLOT(on_currentRowChanged(QModelIndex,QModelIndex))); } MainWindow::~MainWindow() { delete ui; }
此时这个程序运行后会得到表内数据:
接着我们需要绑定TableView
表格的on_currentRowChanged()
事件,当用户点击TableView
表格中的某个属性是则自动触发该函数,在此函数内我们完成对其他组件的填充.
1.通过currentIndex
方法获取到当前表所在行
2.通过当前行号查询表中姓名,并带入StudentTimetable
表查该表中记录
3.循环获取该用户的数据,并将timetable
字段提取出来放入QStringList
容器
4.将数据直接关联到ListView
数据表中
void MainWindow::on_currentRowChanged (const QModelIndex ¤t, const QModelIndex &previous) { Q_UNUSED(previous); if (!current.isValid()) { return ; } dataMapper->setCurrentModelIndex(current); bool first=(current.row()==0 ); bool last=(current.row()==qryModel->rowCount()-1 ); std ::cout << "IsFirst: " << first << "IsLast: " << last << std ::endl ; int curRecNo=theSelection->currentIndex().row(); QSqlRecord curRec=qryModel->record(curRecNo); QString uname = curRec.value("name" ).toString(); std ::cout << "Student Name = " << uname.toStdString() << std ::endl ; QSqlQuery query; query.prepare("select * from StudentTimetable where name = :x" ); query.bindValue(":x" ,uname); query.exec(); QSqlRecord rec = query.record(); QStringList the_data; while (query.next()) { int index = rec.indexOf("timetable" ); QString data = query.value(index).toString(); std ::cout << "User timetable = " << data.toStdString() << std ::endl ; the_data.append(data); } QStringListModel *model; model = new QStringListModel(the_data); ui->listView->setModel(model); ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers); }
当绑定选中事件时,程序运行效果如下:
针对底部按钮处理事件相对来说较为简单,其实现原理就是调用了TableView
默认提供的一些函数而已,代码如下:
void MainWindow::refreshTableView () { int index=dataMapper->currentIndex(); QModelIndex curIndex=qryModel->index(index,0 ); theSelection->clearSelection(); theSelection->setCurrentIndex(curIndex,QItemSelectionModel::Select); } void MainWindow::on_pushButton_clicked () { dataMapper->toFirst(); refreshTableView(); } void MainWindow::on_pushButton_2_clicked () { dataMapper->toLast(); refreshTableView(); } void MainWindow::on_pushButton_3_clicked () { dataMapper->toPrevious(); refreshTableView(); } void MainWindow::on_pushButton_4_clicked () { dataMapper->toNext(); refreshTableView(); }
最终运行效果如下所示: