Qt5.5.0 use MySQL to write small software source code to explain---customer information registration form

Source: Internet
Author: User
Tags prepare

Qt5.5.0 use MySQL to write small software source code to explain---customer information registration form

One person feels relatively simple and small software.

Here is how to write how to publish packaging to introduce it!

Download the MySQL library file link first: Http://files.cnblogs.com/files/xiaobo-Linux/mysql.zip

Paste the two files into the QT directory \qt5.5.0\5.5\mingw492_32\bin folder directly (path according to your own settings)

can also be placed in the original project code, the header file with double quotation marks can be.

<!--wavelet Linux QQ463431476 cnblogs http://www.cnblogs.com/xiaobo-Linux/ --

Enter in the. Pro file


Qt + = SQL

Then perform the following qmake to identify the MySQL

This allows you to reference the library function in the header file:

MySQL statements are used in the same way as sqlite3.

Not many say first, the following look at the source code will understand!

Pro

#-------------------------------------------------# # Project created by wavelet Linux QQ463431476 cnblogs http://www.cnblogs.com/xiaobo-Linux/ qtcreator .- on-29T20: -: -##-------------------------------------------------QT+=Core GUIQT+=Sqlgreaterthan (qt_major_version,4): QT + =Widgetstarget=usersdatatemplate=appsources+=main.cpp mainwindow.cppheaders+=mainwindow.hforms+=mainwindow.uiresources+=Icon.qrcrc_file=icon.rc

. h

#ifndef Mainwindow_h#defineMainwindow_h#include<QMainWindow>#include<QSqlDatabase>//Database Classes#include <QSqlQuery>//EXECUTE statement class#include <QSqlRecord>//return record class#include <QtSql>#include<QApplication>#include<QMessageBox>#include<iostream>#include<QtDebug>//#include <QAbstractTableModel>namespaceUi {classMainWindow;}classMainWindow: Publicqmainwindow{Q_object Public:    ExplicitMainWindow (Qwidget *parent =0); ~MainWindow ();PrivateSlots:voidon_save_clicked (); voidon_del_clicked (); void Select(); voidon_search_clicked (); voidon_back_clicked ();//wavelet Linux QQ463431476 cnblogs http://www.cnblogs.com/xiaobo-Linux/ Private: Ui::mainwindow*UI;};#endif //Mainwindow_h

Main Cpp

Mostly link db files

#include"mainwindow.h"#include<QApplication>intMainintargcChar*argv[])    {Qapplication A (argc, argv); //Create a DB file, create a tableQsqldatabase db = Qsqldatabase::adddatabase ("Qsqlite"); Std::cout<<"begin SQLite"<<Std::endl; Db.setdatabasename ("userdata.db"); if( !Db.open ()) {qmessagebox::critical (NULL, Qobject::tr ("Collection"), Qobject::tr ("failed to connect to database!")); return 0;    } qsqlquery query; Query.exec ("CREATE TABLE UserData (Member number integer PRIMARY KEY, Name text, gender text, registration date text, cell phone number 1 int, mobile number 2 int, note text)");    MainWindow W;    W.show (); returna.exec ();} Wavelet Linux QQ463431476 cnblogs http://www.cnblogs.com/xiaobo-Linux/ 


Mainwindow.cpp

The specific implementation code

Wavelet Linux QQ463431476 Cnblogshttp://www.cnblogs.com/xiaobo-Linux/
#include"mainwindow.h"#include"ui_mainwindow.h"Mainwindow::mainwindow (Qwidget*parent): Qmainwindow (parent), UI (NewUi::mainwindow) {UI-&GT;SETUPUI ( This); Setwindowtitle ("member Information Registration form"); Setwindowicon (Qicon ("Xiaobo.ico"));//set the software icon Select();//Show Table}mainwindow::~MainWindow () {DeleteUI;}voidmainwindow::on_save_clicked () {qsqlquery query; Query.prepare ("INSERT INTO UserData (membership number, name, gender, date of registration, mobile number 1, mobile number 2, remarks) VALUES (: Id,:name,: Sex,:d ate,:p hone1,:p Hone2,:mark)"); Query.bindvalue (": ID",ui->textedit_9->Toplaintext ()); Query.bindvalue (": Name",ui->textedit->Toplaintext ()); Query.bindvalue (": Sex",ui->textedit_5->Toplaintext ()); Query.bindvalue (":d ate",ui->textedit_6->Toplaintext ()); Query.bindvalue (":p hone1",ui->textedit_2->Toplaintext ()); Query.bindvalue (":p Hone2",ui->textedit_3->Toplaintext ()); Query.bindvalue (": Mark",ui->textedit_7->Toplaintext ()); Query.exec (); Select(); Qmessagebox::information ( This,"Tips","user information saved successfully!");}voidmainwindow::on_del_clicked () {qsqlquery query; intCurrow = Ui->tableview->currentindex (). row ();//Mouse Select to delete the first few linesQmodelindex Index= ui->tableview->Currentindex (); intId=index.sibling (Currow,0). Data (). ToInt (); Query.prepare ("Delete from UserData where member Number =: ID"); Query.bindvalue (": ID", id); Query.exec (); Select(); }voidMainWindow::Select() {Qsqlquerymodel*model =NewQsqlquerymodel (ui->TableView); Model->setquery (QString ("Select *from UserData")); UI->tableview->Setmodel (model);} voidmainwindow::on_search_clicked () {qsqlquery query; UI->tableview, Clearspans ();//TableView EmptyQsqlquerymodel *model =NewQsqlquerymodel (ui->TableView); Query.prepare ("Select *from userdata WHERE name =: Name");//Like fuzzy query did not succeedQuery.bindvalue (": Name",ui->textedit_8->text ()); Query.exec (); Model-setquery (query); UI->tableview->Setmodel (model);}voidmainwindow::on_back_clicked () {Select();}

Use of Qt model and TableView: http://www.cnblogs.com/xiaobo-Linux/p/5176432.html

Ui

RC Resource Picture Join

Add ico:http://www.cnblogs.com/xiaobo-linux/p/5176432.html

Software Demo:

Finally, a separate software EXE format is formed: QT uses its own WINDEPLOYQT generation EXE to publish software: http://www.cnblogs.com/xiaobo-Linux/p/4678026.html

The software is then packaged for installation:

Finally, use a packaged software to publish the software!!

That's all.

Qt5.5.0 use MySQL to write small software source code to explain---customer information registration form

Related Article

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.