The writing of Sql,c++ memo software based on Qt5.5.0

Source: Internet
Author: User

My first software.

Qt5.5.0-based SQL, C + + memo software version1.0 writing

The QT version I used is the version of the 5.5.0-free compiler, which is attached with the software I use: http://download.qt.io/official_releases/qt/5.5/5.5.0/download Qt-opensource-windows-x86-mingw492-5.5.0.exe can be.

Not many words, first of all based on the Qt5.5.0 of SQL configuration, here is more detailed in my blog: http://www.cnblogs.com/xiaobo-Linux/p/4674986.html

At this point, the SQL environment is well configured and code is written.

Attach My Code:

++++++++++++++++++project.pro inside the Code ++++++++++++++++

#-------------------------------------------------# # Project created by Qtcreator -- --21T13: A: +##-------------------------------------------------QT+=Core GUIQT+=SQL # Reference Database GreaterThan (qt_major_version,4): QT + =Widgetstarget=project3template=appsources+=main.cpp reminder.cppheaders+=reminder.hforms+=Reminder.ui #DESTDIR+ = C://# Compile the file output path, without this sentence is created under the project file by default. db

+++++++++++++++++++reminder.h's header file code +++++++++++++++++++++++

#ifndef Reminder_h#defineReminder_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>namespaceUi {classReminder;}classReminder: Publicqmainwindow{Q_object Public:    ExplicitReminder (Qwidget *parent =0); ~Reminder ();PrivateSlots:voidon_save_clicked (); voidon_del_clicked (); void Select(); voidon_search_clicked (); voidon_back_clicked ();Private: Ui::reminder*UI;            Qsqldatabase DB; //declaring a database class};#endif //Reminder_h

++++++++++++++++++++++++main.cpp Code +++++++++++++++++++++++++++++

#include"Reminder.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 ("reminder.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 reminder (ID integer PRIMARY KEY autoincrement, subject text, content text)");    Reminder W;    W.show (); returna.exec ();}

+++++++++++++++++reminder.cpp Code +++++++++++++++++

#include"Reminder.h"#include"Ui_reminder.h"Reminder::reminder (Qwidget*parent): Qmainwindow (parent), UI (NewUi::reminder) {UI-&GT;SETUPUI ( This); Setwindowtitle ("wavelet Reminder Memo V1.0"); Select();//Show Table}reminder::~Reminder () {db.close (); DeleteUI;}voidreminder::on_save_clicked () {qsqlquery query; Query.prepare ("INSERT into reminder (ID, subject, Contents) VALUES (1:theme,: content)");//ID automatically increased from 1 onwards without manual insertionQuery.prepare ("INSERT into reminder (subject, Contents) VALUES (: Theme,: Content)"); Query.bindvalue (": Theme", Ui->textedit->text ());//inserting data from an input boxQuery.bindvalue (": Content",ui->textedit_2->Toplaintext ());   Query.exec (); Select();//Show Table}voidreminder::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 reminder where id =: ID"); Query.bindvalue (": ID", id);    Query.exec (); Select(); }voidReminder::Select(){  //displaying the SQL table in TableViewQsqlquerymodel *model =NewQsqlquerymodel (ui->TableView); Model->setquery (QString ("Select *from Reminder")); Model->setheaderdata (0, Qt::horizontal,qobject::tr ("ID")); Model->setheaderdata (1, Qt::horizontal,qobject::tr ("Theme")); Model->setheaderdata (2, Qt::horizontal,qobject::tr ("content")); UI->tableview->Setmodel (model);} voidreminder::on_search_clicked () {qsqlquery query; UITableView, Clearspans ();//TableView EmptyQsqlquerymodel *model =NewQsqlquerymodel (ui->TableView); Query.prepare ("Select *from Reminder where topic =: Theme");//Like fuzzy query did not succeedQuery.bindvalue (": Theme",ui->textedit_3->text ());      Query.exec (); Model-setquery (query); Model->setheaderdata (0, Qt::horizontal,qobject::tr ("ID")); Model->setheaderdata (1, Qt::horizontal,qobject::tr ("Theme")); Model->setheaderdata (2, Qt::horizontal,qobject::tr ("content")); UI->tableview->Setmodel (model);} voidreminder::on_back_clicked () {Select(); }

++++++++++++++++reminder.ui++++++++++++++++++++++++++++

=============== Below is a demonstration of the demo software ================

------------------------the main interface------------------------------

-----------------Search---------------------------

The writing of Sql,c++ memo software based on Qt5.5.0

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.