Qt-how to add, delete, query, modify, and synchronize updates with the mysql database in QSqlQueryModel

Source: Internet
Author: User
I am a Cainiao and am working on a simple record recording system. this is the next interface! IMG (img.ask.csdn.netupload201509201442729281_633123.png )! IMG (img.ask.csdn.netupload201509201442729338_641477.png. The following is my code: #... mysqlqtqsqlquerymodel database

I am a Cainiao working on a simple record recording system,
This is the interface after entering

I want to select a table and click "modify student information". then, I can synchronously refresh the MySQL database after the table is modified and confirmed, and the data in the table is also updated.

The following is my code:
# Include "record. h"
# Include "ui_record.h"
# Include "land. h"
# Include "login. h"
# Include
# Include
# Include
# Include "mysql_object.h"
# Include
# Include
# Include
# Include
# Include

QString str = "";

Record: record (QWidget * parent ):
QDialog (parent ),
Ui (new Ui: record)
{
Ui-> setupUi (this );
Db = MySql_Object: GetInstance ();
QTextCodec: setCodecForTr (QTextCodec: codecForLocale ());

Model = new QSqlQueryModel (this); ui-> tableView-> horizontalHeader ()-> setResizeMode (QHeaderView: Stretch); QString SQL = QString ("select name, age, sex from classmate where id = % 1 "). arg (land: Id); model-> setQuery (SQL); model-> setHeaderData (0, Qt: Horizontal, tr ("classmate name ")); model-> setHeaderData (1, Qt: Horizontal, tr ("age"); model-> setHeaderData (2, Qt: Horizontal, tr ("gender ")); ui-> tableView-> setModel (model );

}

Record ::~ Record ()
{
Delete ui;
}

Void record: on_pushButton_add_2_clicked ()
{
Land * Land = new land ();
Land-> show ();
This-> hide ();
Return;
}

Void record: on_pushButton_change_clicked ()
{
/* QItemSelectionModelSelectionModel = ui-> tableView-> selectionModel ();
QModelIndexList selected = selectionModel-> selectedRows ();
QList rows;
Foreach (const QModelIndex & index, selected)
{
Rows. append (index. row ());
}
Str = model-> data (model-> index (rows [0], 10). toString ();
// Model-> setItemData (str );
Ui-> tableView_2-> setModel (model );
// Int row = ui-> tableView-> currentIndex (). row ();
// Model-> removeRow (row );
/
// Model->
}

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.