QSqlTableModel in action: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:Learning]]
[[Category:Learning]]


'''English''' "French":http://qt-devnet.developpez.com/tutoriels/qsql/qsqltablemodel/
'''English''' "French":http://qt-devnet.developpez.com/tutoriels/qsql/qsqltablemodel/


= QSqlTableModel in action =
= QSqlTableModel in action =
Line 7: Line 7:
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.
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):<br /><code><br />QSqlTableModel* lessons::lessonsTable()<br />{<br /> QSqlTableModel *model = new QSqlTableModel;<br /> model-&gt;setTable(&quot;lessons&amp;quot;);<br /> model-&gt;setEditStrategy(QSqlTableModel::OnFieldChange);<br /> model-&gt;select();<br /> model-&gt;setHeaderData(0, Qt::Horizontal, &quot;ID&amp;quot;);<br /> model-&gt;setHeaderData(1, Qt::Horizontal, &quot;Name&amp;quot;);<br /> model-&gt;setHeaderData(2, Qt::Horizontal, &quot;Teacher&amp;quot;);<br /> return model;<br />}<br /></code><br />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:<br /><code><br />model-&gt;setTable(&quot;lessons&amp;quot;);<br /></code><br />we have a function called &quot;setTable&amp;quot;. Official documentation says about setTable function:
Usually in my every project, I create a pointer function (or whatever its name is):
<code>
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;
}
</code>
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:
<code>
model->setTable("lessons");
</code>
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.''
''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 &quot;select&amp;quot; function, to select the data in the table.
I love Qt's documentation, really. After this function, in line four, we use "select" function, to select the data in the table.


<code><br />model-&gt;setEditStrategy(QSqlTableModel::OnFieldChange);<br /></code><br />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.<br />In Qt we have three edit strategies:<br /># QSqlTableModel::OnFieldChange<br /># QSqlTableModel::OnRowChange<br /># QSqlTableModel::OnManualSubmit
<code>
model->setEditStrategy(QSqlTableModel::OnFieldChange);
</code>
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:
# QSqlTableModel::OnFieldChange
# QSqlTableModel::OnRowChange
# QSqlTableModel::OnManualSubmit


and they are explained very well in Qt's documentation. But I plan to give examples of them later.
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:<br /><code><br />model-&gt;removeColumn(0); // don't show the ID<br /></code><br />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: &quot;QSqlQuery::value: not positioned on a valid record&amp;quot;. Anyway, let's continue.<br /><code><br />model-&gt;setHeaderData(0, Qt::Horizontal, &quot;ID&amp;quot;);<br />model-&gt;setHeaderData(1, Qt::Horizontal, &quot;Name&amp;quot;);<br />model-&gt;setHeaderData(2, Qt::Horizontal, &quot;Teacher&amp;quot;);<br /></code>
In Qt 4.6's QSqlTableModel's class reference document, there is a line that reads:
<code>
model->removeColumn(0); // don't show the ID
</code>
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.
<code>
model->setHeaderData(0, Qt::Horizontal, "ID");
model->setHeaderData(1, Qt::Horizontal, "Name");
model->setHeaderData(2, Qt::Horizontal, "Teacher");
</code>


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.
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:<br /><code><br /> this-&gt;tableModel = lsn.lessonsTable();<br /> ui-&gt;tableView_lessons-&gt;setModel(this-&gt;tableModel);<br /> ui-&gt;tableView_lessons-&gt;setColumnHidden(0, true);<br /></code>
Now let's put the table into a view. I prefer QTableView:
<code>
this->tableModel = lsn.lessonsTable();
ui->tableView_lessons->setModel(this->tableModel);
ui->tableView_lessons->setColumnHidden(0, true);
</code>


'''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.
'''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.

Revision as of 10:07, 25 February 2015


English "French":http://qt-devnet.developpez.com/tutoriels/qsql/qsqltablemodel/

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.