我的第一个软件。
基于Qt5.5.0的 sql ,C++备忘录软件version1.0的编写
我用的Qt版本是5.5.0免配置编译器的版本,这里附上我使用的软件下载地址:http://download.qt.io/official_releases/qt/5.5/5.5.0/ 下载 qt-opensource-windows-x86-mingw492-5.5.0.exe 即可。
话不多说,第一先是基于Qt5.5.0的sql的配置,这里详见我的另外一篇博客:http://www.cnblogs.com/xiaobo-Linux/p/4674986.html
此时,sql环境配置好,编写代码。
附上我的代码:
++++++++++++++++++project.pro里面的代码++++++++++++++++
#------------------------------------------------- # # Project created by QtCreator 2015-07-21T13:12:40 # #------------------------------------------------- QT += core gui QT += sql # 引用数据库 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = project3 TEMPLATE = app SOURCES += main.cpp/ reminder.cpp HEADERS += reminder.h FORMS += reminder.ui #DESTDIR += C:// # 编译文件输出路径,不加此句默认在工程文件下创建.db
+++++++++++++++++++reminder.h的头文件代码+++++++++++++++++++++++
#ifndef REMINDER_H #define REMINDER_H #include <QMainWindow> #include <QSqlDatabase> //数据库类 #include <QSqlQuery> //执行语句类 //#include <QSqlRecord> //返回记录类 #include <QtSql> #include <QApplication> #include <QMessageBox> #include <iostream> //#include <QtDebug> namespace Ui { class Reminder; } class Reminder : public QMainWindow { Q_OBJECT public: explicit Reminder(QWidget *parent = 0); ~Reminder(); private slots: void on_save_clicked(); void on_del_clicked(); void select(); void on_search_clicked(); void on_back_clicked(); private: Ui::Reminder *ui; QSqlDatabase db; //声明数据库类 }; #endif // REMINDER_H
++++++++++++++++++++++++main.cpp代码+++++++++++++++++++++++++++++
#include "reminder.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); //创建DB文件,创建表格 QSqlDatabase 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,主题 text,内容 text)"); Reminder w; w.show(); return a.exec(); }
+++++++++++++++++reminder.cpp代码+++++++++++++++++
#include "reminder.h" #include "ui_reminder.h" Reminder::Reminder(QWidget *parent) : QMainWindow(parent), ui(new Ui::Reminder) { ui->setupUi(this); setWindowTitle("小波的提醒备忘录V1.0"); select();//显示表格 } Reminder::~Reminder() { db.close(); delete ui; } void Reminder::on_save_clicked() { QSqlQuery query; query.prepare("insert into reminder (id,主题,内容)values(1:theme , :content )"); //id自动增加,先从1起,之后无需手动插入 query.prepare("insert into reminder (主题,内容)values(:theme , :content )"); query.bindValue(":theme",ui->textEdit->text()); //从输入框插入数据 query.bindValue(":content",ui->textEdit_2->toPlainText()); query.exec(); select();//显示表格 } void Reminder::on_del_clicked() { QSqlQuery query; int curRow = ui->tableView->currentIndex().row(); //鼠标选择删除第几行 QModelIndex index = ui->tableView->currentIndex(); int id=index.sibling(curRow,0).data().toInt(); query.prepare("delete from reminder where id = :id"); query.bindValue(":id",id); query.exec(); select(); } void Reminder::select() { //将sql表格显示到tableView里 QSqlQueryModel *model = new QSqlQueryModel(ui->tableView); model->setQuery(QString("select *from reminder")); model->setHeaderData(0,Qt::Horizontal,QObject::tr("ID")); model->setHeaderData(1,Qt::Horizontal,QObject::tr("主题")); model->setHeaderData(2,Qt::Horizontal,QObject::tr("内容")); ui->tableView->setModel(model); } void Reminder::on_search_clicked() { QSqlQuery query; ui -> tableView -> clearSpans(); //tableview清空 QSqlQueryModel *model = new QSqlQueryModel(ui->tableView); query.prepare("select *from reminder where 主题 = :theme "); //like模糊查询没成功 query.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("主题")); model->setHeaderData(2,Qt::Horizontal,QObject::tr("内容")); ui->tableView->setModel(model); } void Reminder::on_back_clicked() { select(); }
++++++++++++++++reminder.ui++++++++++++++++++++++++++++
=============== 下面是演示软件的演示情况 ================
------------------------主界面 ------------------------------
-----------------检索---------------------------