我的第一个软件。
基于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/zhaocundang/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
RC_FILE=icon.rc #添加软件图标
+++++++++++++++++++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");
setWindowIcon(QIcon("xiaobo.ico"));//设置软件图标 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++++++++++++++++++++++++++++
=============== 下面是演示软件的演示情况 ================
------------------------主界面 ------------------------------
-----------------检索---------------------------
Qt为生成的软件添加图标的方法:
1. 准备好ico图标文件名字为 xiaobo.ico,最好放在和.pro文件同一个文件夹中
这里在线生成的图标地址:http://www.ico.la/
2.创建一个叫icon.rc的文件,里面写上文本信息IDI_ICON1 ICON " xiaobo.ico",保存好
3.在pro文件中添加代码:RC_FILE=icon.rc
4.在MainWindow的构造函数中添加setWindowIcon(QIcon(" xiaobo.ico"));
5.再重新运行就ok了
======最后来发布软件=========
Qt使用自带的windeployqt 生成exe来发布软件,详见我的另外一篇博客:http://www.cnblogs.com/zhaocundang/p/4678026.html