The Qsqltablemodel class inherits to the Qsqlquerymodel class, which provides an editable data model that reads and writes to a single SQL table, features: Modify, INSERT, delete, query, and sort
Common functions
Qvariant headerdata (int section, qt::orientation Orientation, int role = Qt::D isplayrole) CO NST gets the horizontal head or vertical header title
BOOL Setheaderdata (int section, qt::orientation Orientation, const qvariant & value, int role = qt::editrole) Set the horizontal head or vertical header title
int RowCount (const Qmodelindex & Parent= Qmodelindex ()) const//Return row count
int ColumnCount (const Qmodelindex &Index = qmodelindex ()) const//return number of columns
virtual bool Removecolumns (int column, int count, const Qmodelindex & parent = Qmodelindex ())//model->removecol Umns (0) Delete the first column
BOOL Qsqltablemodel::submitall (),//Commit all modified data, and then modify the data to be saved in the database
void Qsqltablemodel::revertall ()//undo all changes, and if the database has been submitted for modification, it cannot be changed back by undoing the changes.
virtual void Revertrow (int row)//restore the specified row changes
void Qsqltablemodel::setfilter (const QString & Filter)//filter, filter the database by string filter, equivalent to the WHERE statement in SQL
BOOL Qsqltablemodel::select ()//In the condition of filtering and sorting, display the database in the mode table in accordance with the requirements
void Qsqltablemodel::setsort (int column, qt::sortorder order)//Sort operation. Sort by column and Qt::sortorder. Qt::sortorder have ascending and descending
BOOL InsertRow (int row, const Qmodelindex & parent = Qmodelindex ())//Insert Row
BOOL InsertColumn (int column, Constqmodelindex & Parent = Qmodelindex ())//Insert Column
Model->seteditstrategy (Qsqltablemodel::onmanualsubmit); Set save policy to manual commit
I. Displaying data from a table in a database in Qtableview
- Qsqltablemodel *model = new Qsqltablemodel (ParentObject, database); //excerpt from the Help document
- Model->settable ("employee");
- Model->seteditstrategy (Qsqltablemodel::onmanualsubmit);
- Model->select ();
- Model->removecolumn (0); //don ' t show the ID
- Model->setheaderdata (0, Qt::horizontal, tr ("Name"));
- Model->setheaderdata (1, qt::horizontal, tr ("Salary"));
- Qtableview *view = new Qtableview;
- View->setmodel (model);
- View->show ();
Second, modify the data in the Qtableview after the submission, add transaction processing
- Model->database (). transaction (); //Start transaction Operations
- if (Model->submitall ()) //Submit all modified data to the database
- {
- Model->database (). commit (); //Commit succeeds, transaction will really modify database data
- } Else {
- Model->database (). rollback (); //Commit failed, transaction rollback
- Qmessagebox::warning (This, tr ("TableModel"), TR ("Database error:%1″)." Arg (Model->lasterror (). Text ()));
- }
- Model->revertall (); //Undo Changes
Third, the query operation
Equivalent to SQL statement: SELECT * FROM table name WHERE name = "Name variable"
- Model->setfilter (qobject::tr ("name = '%1′"). Arg (name)); //Filter by name
- Model->select (); //Display results
- for (int i = 0; i < Model.rowcount (); ++i)
- {
- QString name = Model.record (i). Value ("name"). ToString ();
- // ... The record of each article is processed here
- }
- When working with large datasets, it is recommended that you specify fields by index
- int primarykeyindex = Model.record (). IndexOf ("id");
- for (int i = 0; i < Model.rowcount (); ++i)
- {
- Qsqlrecord record = Model.record (i);
- QString name = Record.value ("name"). ToString ();
- // ... The record of each article is processed here
- }
Iv. Sorting Operations
- Model->setsort (0,qt::ascendingorder); //id Property, No. 0 column, ascending order, Qt::D escendingorder in descending order
- Model->select ();
v. Insert Operation
- int rowNum = Model->rowcount (); //Gets the number of rows in the table
- int id = Last id+1;
- Model->insertrow (RowNum); //Add a row, or use InsertRows (0,1) to add 1 records in 0 rows, depending on the collation of the table, may move to a different row position than the specified row
- Model->setdata (Model->index (rownum,0), id); //Because the ID is set as the primary key, the ID attribute value must be added to the new row, and the ID field is on column No. 0
- Model->submitall (); //Can be submitted directly
Vi. Delete a record
First, locate the row you want to delete.
- Model.setfilter ("id = 10");
- Model.select ();
- if (model.rowcount () = = 1)
- {
- Model.removerows (0,1) //If you want to delete all records that meet the criteria, change the 1 to Model.rowcount ()
- Model.submitall ();
- }
Delete the selected row in Qtableview
- int currow = Tableview->currentindex (). row ();
- Model->removerow (Currow); //delete a row
Delete the selected multiline in Qtableview
Qabstractitemview:: Selectionmodeselectionmode()const//prototype
Qmodelindexlistqitemselectionmodel:: Selectedindexes()const//prototype
- Qitemselectionmodel *selections = Tableview->selectionmodel (); //Returns the current selection mode
- Qmodelindexlist selecteds = selections->selectedindexes (); //Returns a list of all selected model project indexes
- foreach (Qmodelindex index, selecteds)
- {
- int currow = Index.row (); //Delete all selected rows
- Model->removerow (Currow);
- }
- int ok = qmessagebox::warning (this,tr ("Delete selected Rows!"), TR ("Are you sure you want to delete the rows in the current selection?"), Qmessagebox::yes,qmessagebox:: No);
- if (ok = = Qmessagebox::yes)
- {
- Model->submitall (); //Submit, delete the row in the database
- } Else {
- Model->revertall (); //If not deleted, revoke
- }
vii. Record of updates
Records must be positioned first
- Model.setfilter ("id = 10");
- Model.select ();
- if (model.rowcount () = = 1)
- {
- Model.setdata (Model.index (0,1), Qobject::tr ("Xiao Wang"));
- Model.submitall ();
- }
You can see that this model is very powerful and completely out of SQL statements, and even if you don't understand the database, you can use it for most common operations. This model provides a buffer that can save all modifications to the model before actually writing to the database until we commit the changes. Of course this is also because we set up its save policy at the very beginning:
Model->seteditstrategy (Qsqltablemodel::onmanualsubmit);
Onmanualsubmit shows that we have to submit changes to make them effective. You can save the changes first, and when we execute the commit function, we actually modify the database. Of course, this model is more advanced than the previous model, and all of the previous operations can be performed here.
QT Notes: Database Summary (iii) the SQL model class-qsqltablemodel model