这个示例展示将 Qt 的 SQL 类与模型/视图框架一起使用的方法。
书籍的信息、作者的信息、图书类型的信息保存在数据库的不同表中。
自定义委托实现鼠标按下为图书评级。

构建在内存中的数据库表并插入数据:
- #ifndef INITDB_H
- #define INITDB_H
-
- #include
-
- void addBook(QSqlQuery &q, const QString & 书名, int 年份,
- const QVariant & 作者ID, const QVariant & 类型ID, int 星级)
- {
- q.addBindValue(书名);
- q.addBindValue(年份);
- q.addBindValue(作者ID);
- q.addBindValue(类型ID);
- q.addBindValue(星级);
- q.exec();
- }
-
- QVariant addGenre(QSqlQuery &q, const QString &作品类型名称)
- {
- q.addBindValue(作品类型名称);
- q.exec();
- return q.lastInsertId();
- }
-
- QVariant addAuthor(QSqlQuery &q, const QString &作者名称, QDate 作者生日)
- {
- q.addBindValue(作者名称);
- q.addBindValue(作者生日);
- q.exec();
- return q.lastInsertId();
- }
-
- const auto BOOKS_SQL = R"(create table 书籍(id integer primary key,
- 书名 varchar,
- 作者 integer,
- 类型 integer,
- 年份 integer,
- 星级 integer))";
-
- const auto AUTHORS_SQL = R"(create table 作家(id integer primary key,
- 姓名 varchar,
- 生日 date))";
-
- const auto GENRES_SQL = R"(create table 作品类型(id integer primary key,
- 类型名称 varchar))";
-
- const auto INSERT_AUTHOR_SQL = R"(insert into 作家(姓名, 生日)
- values(?, ?))";
-
- const auto INSERT_BOOK_SQL = R"(insert into 书籍(书名, 年份, 作者, 类型, 星级)
- values(?, ?, ?, ?, ?))";
-
- const auto INSERT_GENRE_SQL = R"(insert into 作品类型(类型名称)
- values(?))";
-
- QSqlError initDb()
- {
- QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
- db.setDatabaseName(":memory:");
-
- if (!db.open())
- return db.lastError();
-
- QStringList tables = db.tables();
- if (tables.contains("书籍", Qt::CaseInsensitive)
- && tables.contains("作家", Qt::CaseInsensitive))
- return QSqlError();
-
- QSqlQuery q;
- if (!q.exec(BOOKS_SQL))
- return q.lastError();
- if (!q.exec(AUTHORS_SQL))
- return q.lastError();
- if (!q.exec(GENRES_SQL))
- return q.lastError();
-
- if (!q.prepare(INSERT_AUTHOR_SQL))
- return q.lastError();
-
- QVariant asimovId = addAuthor(q, QLatin1String("艾萨克.阿西莫夫").data(), QDate(1920, 2, 1));
- QVariant greeneId = addAuthor(q, QLatin1String("格雷厄姆·格林").data(), QDate(1904, 10, 2));
- QVariant pratchettId = addAuthor(q, QLatin1String("特里·普拉切特").data(), QDate(1948, 4, 28));
-
- if (!q.prepare(INSERT_GENRE_SQL))
- return q.lastError();
-
- QVariant sfiction = addGenre(q, QLatin1String("科幻小说").data());
- QVariant fiction = addGenre(q, QLatin1String("幽默讽刺小说").data());
- QVariant fantasy = addGenre(q, QLatin1String("奇幻小说").data());
-
- if (!q.prepare(INSERT_BOOK_SQL))
- return q.lastError();
-
- addBook(q, QLatin1String("基地").data(), 1951, asimovId, sfiction, 3);
- addBook(q, QLatin1String("基地与帝国").data(), 1952, asimovId, sfiction, 4);
- addBook(q, QLatin1String("第二基地").data(), 1953, asimovId, sfiction, 3);
- addBook(q, QLatin1String("基金边缘").data(), 1982, asimovId, sfiction, 3);
- addBook(q, QLatin1String("基地与地球").data(), 1986, asimovId, sfiction, 4);
- addBook(q, QLatin1String("基地前奏").data(), 1988, asimovId, sfiction, 3);
- addBook(q, QLatin1String("迈向基地").data(), 1993, asimovId, sfiction, 3);
- addBook(q, QLatin1String("权力与荣耀").data(), 1940, greeneId, fiction, 4);
- addBook(q, QLatin1String("第三个人").data(), 1950, greeneId, fiction, 5);
- addBook(q, QLatin1String("我们在哈瓦那的人").data(), 1958, greeneId, fiction, 4);
- addBook(q, QLatin1String("卫兵!卫兵!").data(), 1989, pratchettId, fantasy, 3);
- addBook(q, QLatin1String("守夜人").data(), 2002, pratchettId, fantasy, 3);
- addBook(q, QLatin1String("邮局奇遇记").data(), 2004, pratchettId, fantasy, 3);
-
- return QSqlError();
- }
-
- #endif
构建了书籍、作家、作品类型三个数据表并插入相应的数据。
简单的UI界面:

使用模型显示数据库中的数据:
- BookWindow::BookWindow()
- {
- ui.setupUi(this);
-
- if (!QSqlDatabase::drivers().contains("QSQLITE"))
- QMessageBox::critical(this,"无法加载数据库","此示例需要 SQLITE 驱动程序");
-
- QSqlError err = initDb();
- if (err.type() != QSqlError::NoError)
- {
- QMessageBox::critical(this, "无法初始化数据库","错误信息:" + err.text());
- return;
- }
-
- model = new QSqlRelationalTableModel(ui.bookTable);
- model->setEditStrategy(QSqlTableModel::OnManualSubmit);
- model->setTable("书籍");
-
- //“书籍”表中相关索引
- authorIdx = model->fieldIndex("作者");
- genreIdx = model->fieldIndex("类型");
-
- //设置与其他数据库表的关系:
- model->setRelation(authorIdx, QSqlRelation("作家", "id", "姓名"));
- model->setRelation(genreIdx, QSqlRelation("作品类型", "id", "类型名称"));
-
- //设置本地化的标题
- model->setHeaderData(authorIdx, Qt::Horizontal, tr("_作者_"));
- model->setHeaderData(genreIdx, Qt::Horizontal, tr("_类型_"));
- model->setHeaderData(model->fieldIndex("书名"),Qt::Horizontal, tr("_书名_"));
- model->setHeaderData(model->fieldIndex("年份"), Qt::Horizontal, tr("_年份_"));
- model->setHeaderData(model->fieldIndex("星级"),Qt::Horizontal, tr("_星级_"));
-
- if (!model->select())
- {
- QMessageBox::critical(this, "无法初始化数据库","错误信息:" + model->lastError().text());
- return;
- }
-
- //设置模型并隐藏 ID 列:
- ui.bookTable->setModel(model);
- ui.bookTable->setItemDelegate(new BookDelegate(ui.bookTable));
- ui.bookTable->setColumnHidden(model->fieldIndex("id"), true);
- ui.bookTable->setSelectionMode(QAbstractItemView::SingleSelection);
-
- //使用作者姓名填充QComboBox
- ui.authorEdit->setModel(model->relationModel(authorIdx));
- ui.authorEdit->setModelColumn(model->relationModel(authorIdx)->fieldIndex("姓名"));
-
- //使用小说类型填充QComboBox
- ui.genreEdit->setModel(model->relationModel(genreIdx));
- ui.genreEdit->setModelColumn(model->relationModel(genreIdx)->fieldIndex("类型名称"));
-
- //此列拉伸
- ui.bookTable->horizontalHeader()->setSectionResizeMode(model->fieldIndex("星级"),QHeaderView::Stretch);
-
- auto mapper = new QDataWidgetMapper(this);
- mapper->setModel(model);
- mapper->setItemDelegate(new BookDelegate(this));
- mapper->addMapping(ui.titleEdit, model->fieldIndex("书名"));
- mapper->addMapping(ui.yearEdit, model->fieldIndex("年份"));
- mapper->addMapping(ui.authorEdit, authorIdx);
- mapper->addMapping(ui.genreEdit, genreIdx);
- mapper->addMapping(ui.ratingEdit, model->fieldIndex("星级"));
-
- ui.ratingEdit->setSuffix(" 星");
-
- connect(ui.bookTable->selectionModel(),&QItemSelectionModel::currentRowChanged,
- mapper,&QDataWidgetMapper::setCurrentModelIndex);
-
- ui.bookTable->setCurrentIndex(model->index(0, 0));
- }
使用的是 QSqlRelationalTableModel 模型,它可为单个数据库表提供可编辑的数据模型,并允许将列设置为其他数据库表的外键。
设置此模型显示 “书籍” 表中的数据,并设置:
- //“书籍”表中相关索引
- authorIdx = model->fieldIndex("作者");
- genreIdx = model->fieldIndex("类型");
-
- //设置与其他数据库表的关系:
- model->setRelation(authorIdx, QSqlRelation("作家", "id", "姓名"));
- model->setRelation(genreIdx, QSqlRelation("作品类型", "id", "类型名称"));
最后使用 QDataWidgetMapper 使模型和小部件之间建立对应关系,它可以用于显示模型中的数据并将小部件中数据的更改反馈到模型。
关于第五列的星形委托见:自定义委托(星形委托)
涉及到的知识点和类: