QSqlTableModel in action

From Qt Wiki
Jump to navigation Jump to search
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

English French

QSqlTableModel in action

As a programmer who started his programming life with PHP and web technologies, QSqlTableModel is a great class to interact with database tables. It is very easy to insert, delete or update an entry with it.

Usually in my every project, I create a pointer function (or whatever its name is):

QSqlTableModel* lessons::lessonsTable()
{
 QSqlTableModel *model = new QSqlTableModel;
 model->setTable("lessons");
 model->setEditStrategy(QSqlTableModel::OnFieldChange);
 model->select();
 model->setHeaderData(0, Qt::Horizontal, "ID");
 model->setHeaderData(1, Qt::Horizontal, "Name");
 model->setHeaderData(2, Qt::Horizontal, "Teacher");
 return model;
}

This code is from one of my projects which won a silver medal in an international informatics contest. Anyway, let's look at the code. In the second line:

model->setTable("lessons");

we have a function called "setTable". Official documentation says about setTable function:

Sets the database table on which the model operates to tableName. Does not select data from the table, but fetches its field information.

I love Qt's documentation, really. After this function, in line four, we use "select" function, to select the data in the table.

model->setEditStrategy(QSqlTableModel::OnFieldChange);

The function in the line above sets the strategy for editing values in the database to one of various editing strategies in Qt. But be careful, this will revert any pending changes. In Qt we have three edit strategies:

  1. QSqlTableModel::OnFieldChange
  2. QSqlTableModel::OnRowChange
  3. QSqlTableModel::OnManualSubmit

and they are explained very well in Qt's documentation. But I plan to give examples of them later.

In Qt 4.6's QSqlTableModel's class reference document, there is a line that reads:

model->removeColumn(0); // don't show the ID

But I prefer to hide a column instead of removing it. If you remove a column, when you try to edit the entries in the table via a table view, it says: "QSqlQuery::value: not positioned on a valid record". Anyway, let's continue.

model->setHeaderData(0, Qt::Horizontal, "ID");
model->setHeaderData(1, Qt::Horizontal, "Name");
model->setHeaderData(2, Qt::Horizontal, "Teacher");

These three line helps you to make more explanatory interfaces by setting a caption. It is useful when you use a view, e.g. QTableView.

Now let's put the table into a view. I prefer QTableView:

 this->tableModel = lsn.lessonsTable();
 ui->tableView_lessons->setModel(this->tableModel);
 ui->tableView_lessons->setColumnHidden(0, true);

lsn is a class I created and it has the function I wrote above. As I mentioned above, I prefer to hide the column I don't want to show so I do that in the third line.