Qt is a small management software based on sqlite database and qtsqlite database.

Source: Internet
Author: User

Qt is a small management software based on sqlite database and qtsqlite database.

I wrote a sqlite-based database management software.

First

 

The central idea is:

Create a database, and each group corresponds to a data table, and then traverse the data table. Display the name in treewidgetItem. Adding or deleting a database is essentially an operation on the database. If you don't want to write too many functions, you will become the Database Desktop Management edition ).

Below is the code:

Administrater: Administrater (QWidget * parent): QMainWindow (parent) {QSqlDatabase Administrater = QSqlDatabase: addDatabase ("QSQLITE"); Administrater. setDatabaseName ("Administrater. db "); Administrater. open (); _ query = QSqlQuery (Administrater); _ query. prepare ("create table goldVip (name varchar (20), sex varchar (10), ID char (20)"); _query.exe c (); _ query. prepare ("create table Vip (name varchar (50), sex varcha R (10), ID char (20) "); _query.exe c (); _ query. prepare ("create table Coustomer (name varchar (20), sex varchar (10), ID char (20)"); _query.exe c (); _ query. prepare ("select count (*) from goldVip"); _query.exe c (); _ query. next (); _ a = _ query. value (0 ). toInt (); _ query. prepare ("select count (*) from Vip"); _query.exe c (); _ query. next (); _ B = _ query. value (0 ). toInt (); _ query. prepare ("select count (*) from Coustomer"); _ q Uery.exe c (); _ query. next (); _ c = _ query. value (0 ). toInt (); this-> setGeometry (400,200,600,400); _ treeWidget = new QTreeWidget (this); _ treeWidget-> setColumnCount (1 ); _ treeWidget-> setHeaderLabel ("Contact"); _ treeWidget-> setStyleSheet ("color: blue;" "background-color: yellow;" "selection-color: yellow; "" selection-background-color: blue; "); _ Item = new QTreeWidgetItem (_ treeWidget); _ Item-> setText (0," golden vip ");/ /_ Item-> setCheckState (0, Qt: Checked); // check box _ Item-> setIcon (0, QIcon ("1.ico"); int I = 0; _ query. prepare ("select * from goldVip"); _query.exe c (); while (I <_ a) {_ query. next (); QTreeWidgetItem * Item = new QTreeWidgetItem; Item-> setText (0, _ query. value (0 ). toString (); _ Item-> addChild (Item); ++ I;} // connect (_ Item, SIGNAL (itemClicked (QTreeWidgetItem * Item, int index )), this, // SLOT (slotItem (QTreeWidgetItem * Item, int index); _ Item2 = new QTreeWidgetItem (_ treeWidget); _ Item2-> setText (0, "ordinary vip "); _ Item2-> setIcon (0, QIcon ("2.ico"); I = 0; _ query. prepare ("select * from Vip"); _query.exe c (); while (I <_ B) {_ query. next (); QTreeWidgetItem * Item = new QTreeWidgetItem; Item-> setText (0, _ query. value (0 ). toString (); _ Item2-> addChild (Item); ++ I;} _ Item3 = new QTreeWidgetItem (_ treeWidget); _ Item3-> setText (0, "Common customer"); _ It Em3-> setIcon (0, QIcon ("3.ico"); I = 0; _ query. prepare ("select * from Coustomer"); _query.exe c (); while (I <_ c) {_ query. next (); QTreeWidgetItem * Item = new QTreeWidgetItem; Item-> setText (0, _ query. value (0 ). toString (); _ Item3-> addChild (Item); ++ I;} // Administrater. close (); QMenuBar * menuBar = QMainWindow: menuBar (); this-> setMenuBar (menuBar); QAction * Action = new QAction ("add", this ); QAction * Action2 = new QA Ction ("delete", this); QAction * Action3 = new QAction ("query", this); QList <QAction *> _ list; _ list <Action <Action2 <Action3; menuBar-> addActions (_ list); connect (Action, SIGNAL (triggered (bool), this, SLOT (slotAction (); connect (Action2, SIGNAL (triggered (bool), this, SLOT (slotAction2 (); connect (Action3, SIGNAL (triggered (bool), this, SLOT (slotAction3 ();} Administrater ::~ Administrater () {} void Administrater: resizeEvent (QResizeEvent *) {_ treeWidget-> setGeometry (0, 0, this-> width (), this-> height ());} void Administrater: slotAction2 () {Delete * w2 = new Delete; w2-> show (); connect (w2, SIGNAL (delete_sucess (), this, SLOT (update_show ();} void Administrater: slotAction () {Deal * w = new Deal; w-> show (); w-> setStyleSheet ("background-color: rgb (255,252,162);"); connect (w, SIGNAL (add_sucess (), SLOT (update_show ()));} void Administrater: slotAction3 () {Find * w3 = new Find; w3-> show ();} void Administrater: update_show () {delete _ Item; delete _ Item2; delete _ Item3; _ Item = new QTreeWidgetItem (_ treeWidget); _ Item-> setText (0, "golden vip"); // _ Item-> setCheckState (0, Qt:: Checked); // check box _ Item-> setIcon (0, QIcon ("1.ico"); _ Item2 = new QTreeWidgetItem (_ treeWidget); _ Item2-> setText (0, "ordinary vip"); _ Item2-> setIcon (0, QIcon ("2.ico"); _ Item3 = new QTreeWidgetItem (_ treeWidget); _ Item3-> setText (0, "Common customer"); _ Item3-> setIcon (0, QIcon ("3.ico"); _ query. prepare ("select count (*) from goldVip"); _query.exe c (); _ query. next (); _ a = _ query. value (0 ). toInt (); _ query. prepare ("select count (*) from Vip"); _query.exe c (); _ query. next (); _ B = _ query. value (0 ). toInt (); _ query. prepare ("select count (*) from Coustomer"); _query.exe c (); _ query. next (); _ c = _ query. value (0 ). toInt (); // _ Item-> removeChild (); int I = 0; _ query. prepare ("select * from goldVip"); _query.exe c (); while (I <_ a) {_ query. next (); QTreeWidgetItem * Item = new QTreeWidgetItem; Item-> setText (0, _ query. value (0 ). toString (); _ Item-> addChild (Item); ++ I;} // connect (_ Item, SIGNAL (itemClicked (QTreeWidgetItem * Item, int index )), this, // SLOT (slotItem (QTreeWidgetItem * Item, int index); I = 0; _ query. prepare ("select * from Vip"); _query.exe c (); while (I <_ B) {_ query. next (); QTreeWidgetItem * Item = new QTreeWidgetItem; Item-> setText (0, _ query. value (0 ). toString (); _ Item2-> addChild (Item); ++ I;} I = 0; _ query. prepare ("select * from Coustomer"); _query.exe c (); while (I <_ c) {_ query. next (); QTreeWidgetItem * Item = new QTreeWidgetItem; Item-> setText (0, _ query. value (0 ). toString (); _ Item3-> addChild (Item); ++ I ;}}
# Include "Deal. h "# include" ui_Deal.h "# include <QtSql/QSqlDatabase> # include <QSqlQuery> # include <QDebug> # include <QComboBox> # include <QMessageBox> Deal :: deal (QWidget * parent): QWidget (parent), ui (new Ui: Deal) {ui-> setupUi (this); connect (ui-> comboBox, SIGNAL (activated (int), this, SLOT (on_comboBox_activated (int d); this-> setStyleSheet ("QLineEdit {background-color: red }"); ui-> lineEdit-> setStyleSheet ("back Ground-color: rgb (188,255,207); "); ui-> lineEdit_2-> setStyleSheet (" background-color: rgb (188,255,207 );"); ui-> lineEdit_3-> setStyleSheet ("background-color: rgb (188,255,207);");} Deal ::~ Deal () {delete ui;} void Deal: on_pushButton_clicked () {QSqlQuery query; QString str = ui-> lineEdit-> text (); QString str2 = ui-> lineEdit_2-> text (); QString str3 = ui-> lineEdit_3-> text (); if (_ Kind. isEmpty () {QMessageBox: information (this, "tip", "You have not selected customer category", QMessageBox: OK); return ;} QString str4 = QString ("insert into") + _ Kind + QString ("values (") + QString ("\ '") + str + QString ("\'") + QString (",") + QString ("\ '") + str2 + QString ("\'") + QString (",") + QString ("\'") + str3 + QString ("\ '") + QString (")"); query. prepare (str4); if(query.exe c () {QMessageBox * messageBox = new QMessageBox (QMessageBox: Information, "tip", "added successfully", QMessageBox: OK ); messageBox-> show (); _ timer = new QTimer (this); _ timer-> start (1200); connect (_ timer, SIGNAL (timeout (), messageBox, SLOT (close (); emit add_sucess ();} else {qDebug () <"falied" ;}} void Deal: on_comboBox_activated (int s) {if (ui-> lineEdit-> text (). isEmpty () | ui-> lineEdit_2-> text (). isEmpty () | ui-> lineEdit_3-> text (). isEmpty () {QMessageBox: information (this, "Tip:", "the information you entered is incomplete", QMessageBox: OK );} if (s = 0) {_ Kind = "goldVip";} else if (s = 1) {_ Kind = "Vip ";} else if (s = 2) {_ Kind = "Coustomer ";}}

  

# Include "Delete. h "# include" ui_Delete.h "# include <QSqlQuery> # include <QDebug> # include <QMessageBox> # include <QTimer> # include <QPalette> Delete :: delete (QWidget * parent): QWidget (parent), ui (new Ui: Delete) {ui-> setupUi (this);} Delete ::~ Delete () {delete ui;} void Delete: on_pushButton_clicked () {QString str = ui-> lineEdit-> text (); QString str2 = "delete from "; QString str3 = "where name ="; QString str4 = str2 + _ Str + str3 + QString ("\ '") + str + QString ("\'"); QSqlQuery query; query. prepare (str4); if(query.exe c () {QMessageBox * messageBox = new QMessageBox (QMessageBox: Information, "tip", "deleted successfully", QMessageBox: OK ); messageBox-> show (); QTimer * timer = new QTimer (this); timer-> start (1200); connect (timer, SIGNAL (timeout (), messageBox, SLOT (close (); emit delete_sucess ();} else {QMessageBox * messageBox = new QMessageBox (QMessageBox: Information, "tip ", "This user is not in this group", QMessageBox: OK) ;}} void Delete: on_comboBox_activated (int index) {if (index = 0) {_ Str = "goldVip";} else if (index = 1) {_ Str = "Vip";} else if (index = 2) {_ Str = "Coustomer ";}}

  

# Include "Find. h "# include" ui_Find.h "# include <QSqlQuery> # include <QMessageBox> # include <QPalette> Find: Find (QWidget * parent): QWidget (parent ), ui (new Ui: Find) {ui-> setupUi (this); _ lists <"name:" <"Gender:" <"ID :"; QPalette pal = this-> palette (); pal. setBrush (QPalette: Background, Qt: green); this-> setPalette (pal);} Find ::~ Find () {delete ui;} void Find: on_pushButton_clicked () {if (_ Str. isEmpty () {QMessageBox: information (this, "warning", "You have not selected the customer type", QMessageBox: OK );} QString str = ui-> lineEdit-> text (); QString str2 = "select * from" + _ Str + QString ("where name = ") + QString ("\ '") + str + QString ("\'"); QSqlQuery query; query. prepare (str2); if(query.exe c () {query. next (); if (query. value (0 ). toString (). isEmpty () {QMessageBox: warni Ng (this, "warning", "this database does not query this person", QMessageBox: OK); return;} while (! Query. value (0 ). toString (). isEmpty () {for (int I = 0; I <3; ++ I) {ui-> textBrowser-> append (_ lists. at (I) + query. value (I ). toString ();} query. next () ;}} else {QMessageBox * messageBox = new QMessageBox (QMessageBox: Information, "tip", "No user in this group", QMessageBox :: OK) ;}} void Find: on_comboBox_activated (int index) {if (index = 0) {_ Str = "goldVip";} else if (index = 1) {_ Str = "Vip";} else if (index = 2) {_ Str = "Coustomer ";}}

By the way, sqlite does not have a header. Therefore, when we use query to extract data, we can only use next to obtain data.

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.