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:
'''English''' [http://qt-devnet.developpez.com/tutoriels/qsql/qsqltablemodel/ French] ''[qt-devnet.developpez.com]''
[[Category:Learning]]


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


As a programmer who started his programming life with <span class="caps">PHP</span> 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.
= QSqlTableModel in action =


Usually in my every project, I create a pointer function (or whatever its name is):<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 /> we have a function called “setTable”. Official documentation says about setTable function:
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:


''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 “select” function, to select the data in the table.
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.


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:
<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


# QSqlTableModel::OnFieldChange
and they are explained very well in Qt's documentation. But I plan to give examples of them later.
# QSqlTableModel::OnRowChange
# 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:<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:<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: “QSqlQuery::value: not positioned on a valid record”. Anyway, let’s continue.<br />


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 />
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>
 
'''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.
 
I’m going to write about insert, delete and the other things about QSqlTableModel soon.
 
===Categories:===


* [[:Category:Learning|Learning]]
'''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 09:47, 24 February 2015


English "French&quot;: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):

<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 />


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 />model-&gt;setTable(&quot;lessons&amp;quot;);<br />


we have a function called "setTable&quot;. 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&quot; function, to select the data in the table.

<br />model-&gt;setEditStrategy(QSqlTableModel::OnFieldChange);<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.
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.

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

<br />model-&gt;removeColumn(0); // don't show the ID<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: "QSqlQuery::value: not positioned on a valid record&quot;. Anyway, let's continue.

<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 />

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 /> 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 />

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.