Qt notes: Database Summary (2) SQL model-qsqlquerymodel

Source: Internet
Author: User

The qsqlquerymodel class provides a read-only data model for the SQL result set. Next we will use this class for the simplest operation.

Common functions

Void qsqlquerymodel: setquery ("SQL statement") // executes an SQL statement. You can also input a qsqlquery object. In this case, you can use some features of the qsqlquery class, such as pre-operations.

Setheaderdata () // sets the horizontal header title

Columncount (); // obtain the number of Columns

Columncount (); // obtain the number of Columns

Qsqlrecord qsqlquerymodel: Record (INT row) const // return the information contained in the row to access a single record

Qmodelindex q1_actitemmodel: Index (INT row, int column, const qmodelindex & parent = qmodelindex () // return the index of the specified row and column)

Index. Data () // returns the index value

Query () // return the qsqlquery-related model

QSqlQueryModel *model = new QSqlQueryModel;model->setQuery(“select * from student”);model->setHeaderData(0, Qt::Horizontal, tr(“id”));model->setHeaderData(1, Qt::Horizontal, tr(“name”));QTableView *view = new QTableView;view->setModel(model);view->show();

 

Use query to execute SQL statements

Qsqlquery query = model-> query();query.exe C ("Select name from student where id = 1"); query. next (); qdebug () <query. value (0 ). tostring (); // if the preceding SELECT statement is changed to an insert statement and is displayed in qtableview, You need to query model-> setquery ("select... ") to display the just-inserted statement

Because the qsqlquerymode model is read-only by default, we cannot modify the content in the table in the window. However, we can create our own models, display data and modify data as needed. If you want to make it readable and writable, your class must inherit from qsqlquerymodel and rewrite the setdata () and flags () functions. If you want to change the display of data, you must rewrite data () function.

Qt: itemflags mysqlquerymodel: Flags (const qmodelindex & Index) const // returns the variable flag of the table {QT: itemflags flags = qsqlquerymodel: Flags (INDEX ); if (index. column () = 1) // The second field can be changed, that is, the student Name field flags | = QT: itemiseditable; return flags;} bool mysqlquerymodel :: setdata (const qmodelindex & Index, const qvariant & Value, INT/* role */) // Add data to a table {qmodelindex primarykeyindex = qsqlquerymodel: Index (index. row (), 0); int id = data (primarykeyindex ). toint (); // you can use this method to obtain the ID, primarykeyindex. data (); clear (); bool isok; If (index. column () = 1) // The second attribute can be changed {qsqlquery query; query. prepare ("Update student set name =: name where id =: ID"); query. bindvalue (": Name", "5th"); query. bindvalue (": ID", ID); isok = query.exe C (); refresh (); // You are advised to add the code to refresh the result, or call the refresh function return isok here ;} return false;} void mysqlquerymodel: refresh () // update display {setquery ("select * from student"); setheaderdata (0, QT: horizontal, qobject :: TR ("student ID"); setheaderdata (1, QT: horizontal, qobject: TR ("name "));}//
// Tata () rewrite the display style of a column, alignment qvariant mysqlquerymodel: Data (const qmodelindex & Index, int role) const {qvariant value = qsqlquerymodel :: data (index, role); If (role = QT: textcolorrole & Index. column () = 0) return qvariantfromvalue (qcolor (QT: Red); // The font color of the first attribute is red if (role = QT :: textalignmentrole & Index. column () = 1) {value = (QT: alignvcenter + QT: alignright); // center right vertically} return value ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.