[Qt Tutorial] database (six) SQL relational table model QSqlRelationalTableMode

Lead
QSqlRelationalTableModel inherited from QSqlTableModel, and it has been extended to provide support for the foreign key. A foreign key is a one to one mapping between the attributes in a table and other tables primary key attribute. For example, student attribute table of course is a course table corresponding to the id attribute, then the attribute course is called a foreign key. Because the value of the property course here are some numbers, such a display is very friendly relationship model using a table, you can show it to the value of the name attribute of course the table.

Environment: Windows Xp + Qt 4.8.4 + QtCreator 2.6.2

Catalog
One, the use of foreign keys
Second, the use commission

text

First, a foreign key

1. Create a new Qt Gui application entitled relationalTableModel, The QMainWindow base class, the class name MainWindow. After completion of the open relationalTableModel.pro project file, the first line read:
QT + = coregui SQL
and then save the file.

2.下面向项目中添加新的C++头文件connection.h,并更改其内容如下:
#ifndef CONNECTION_H
#define CONNECTION_H
#include <QSqlDatabase>
#include <QSqlQuery>
static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("database.db");
if(!db.open()) return false;
QSqlQuery query;
query.exec("create table student (id int primarykey, name vchar,course int)");
query.exec("insert into student values(1,'yafei0',1)");
query.exec("insert into student values(2,'yafei1',1)");
query.exec("insert into student values(3,'yafei2',2)");

query.exec("create table course (id int primarykey, name vchar, teacher vchar)");
query.exec("insert into course values(1,'Math','yafeilinux1')");
query.exec("insert into course values(2,'English','yafeilinux2')");
query.exec("insert into course values(3,'Computer','yafeilinux3')");
return true;
}
#endif // CONNECTION_H

   在这里建立了两个表,student表中有一项是course,它是int型的,而course表的主键也是int型的。如果要将course项和course表进行关联,它们的类型就必须相同,一定要注意这一点。

3.更改main.cpp文件内容如下:
#include "mainwindow.h"
#include <QApplication>
#include "connection.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
if(!createConnection()) return 1;
MainWindow w;
w.show();

return a.exec();

}

Mainwindow.hw member 4. Then the first header comprising:
#include <QSqlRelationalTableModel acts>
then add the private type objects declared:
QSqlRelationalTableModel acts * Model;

5. to design mode, a drag and drop interface to Table View member.

6. mainwindow.cpp the file, add the following code in the constructor:
Model = new new QSqlRelationalTableModel acts (the this);
write database attribute change //
Model-> setEditStrategy (QSqlTableModel :: OnFieldChange);
Model-> setTable ( " student ");
// the third attribute id attribute is set to the student table course foreign key table,
// and displays the name attribute value table course
model-> setRelation (2, QSqlRelation ( " course "," ID "," name "));
Model-> setHeaderData (0, the Qt :: Horizontal, QObject :: TR (" ID "));
Model-> setHeaderData (. 1, the Qt :: Horizontal, QObject :: TR ( "the Name"));
Model-> setHeaderData (2, the Qt :: Horizontal, QObject :: TR ( "Course,"));
Model-> SELECT ();
ui-> tableView-> setModel to (Model);

   这里修改了model的提交策略,OnFieldChange表示只要属性被改动就马上写入数据库,这样就不需要我们再执行提交函数了。setRelation()函数实现了创建外键,注意它的格式就行了。

7. Run the program, results as shown in FIG.

26-1.jpg

   可以看到Course属性已经不再是编号,而是具体的课程了。关于外键,大家也应该有一定的认识了吧,说简单点就是将两个相关的表建立一个桥梁,让它们关联起来。

Second, the use commission

   有时我们也希望,如果用户更改课程属性,那么只能在课程表中有的课程中进行选择,而不能随意填写课程。Qt中还提供了一个QSqlRelationalDelegate委托类,它可以为QSqlRelationalTableModel显示和编辑数据。这个委托为一个外键提供了一个QComboBox部件来显示所有可选的数据,这样就显得更加人性化了。使用这个委托是很简单的,我们先在mainwindow.cpp文件中添加头文件#include <QSqlRelationalDelegate>,然后继续在构造函数中添加如下一行代码:

ui->tableView->setItemDelegate(
new QSqlRelationalDelegate(ui->tableView));

   运行程序,效果如下图所示。

26-2.jpg

Conclusion
We can choose which model to use according to their needs. If you are familiar with SQL syntax, and do not need all the data are displayed, you only need to use QSqlQuery it. For QSqlTableModel, it is mainly used to display a single table, and can be used to display any QSqlQueryModel a result set, if you want to display any one result set, and the like so that it can read and write, it is recommended that subclass QSqlQueryModel, then reimplement flags () and setData () function. For more information, please see the "Qt Creator Quick Start" in Chapter 17.

Guess you like

Origin blog.51cto.com/14527980/2437480