作者:小波

QQ:463431476

请关注我的博客园:http://www.cnblogs.com/xiaobo-Linux/

我的第二款软件:CET四六级单词背诵软件。基于QT5.5.0、sql数据库、以及TTS文本识别语音单词朗读的一款软件。

第一款软件的sql数据库软件的编写:http://www.cnblogs.com/xiaobo-Linux/p/4676950.html

现在来讲解我的第二款高大上的代码:

这个是.pro的代码,其中axcontainer 是调用的win7 SDK语音库必须的;QT += sql 是引用sql数据库必须的;RC_FILE=icon.rc引用的ico图标文件必须的。

  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator --28T13::
  4. #
  5. #-------------------------------------------------
  6.  
  7. QT += core gui axcontainer #axcontainer speak
  8. QT += sql #sql
  9.  
  10. greaterThan(QT_MAJOR_VERSION, ): QT += widgets
  11.  
  12. TARGET = CET_words
  13. TEMPLATE = app
  14.  
  15. SOURCES += main.cpp\
  16. mainwindow.cpp
  17.  
  18. HEADERS += mainwindow.h
  19.  
  20. FORMS += mainwindow.ui
  21.  
  22. RC_FILE=icon.rc #ico

说起调用系统语音实现TTS也就是text to speech 文本识别成语音朗读,Qt调用win7系统语音库的话可能还得安装SDK语音库,这里附上win7官网sdk下载地址:

http://www.microsoft.com/en-us/download/details.aspx?id=10121

这里选择如图所示:

下载完后才发现本程序可能不需要。不过最好 下载好并解压后安装。

第二个是主窗口头文件mainwindow.h的代码。有引用的数据库的头文件:#include <QSqlDatabase>  #include <QSqlQuery>  #include <QSqlRecord>;

时间的头文件QTimer用于时间停顿;windows.h好像用不着也可以调用系统sleep()函数来时间停顿。QSqlDatabase db;      //声明数据库类 用于sql数据库  #include <QAxObject>  和 QAxObject * voiceObj  是调用win7系统SDK语音库的接口。

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include <QAxObject> //speak com
  5. #include <QSqlDatabase> //数据库类
  6. #include <QSqlQuery> //执行语句类
  7. #include <QSqlRecord> //返回记录类
  8. #include <QtSql>
  9. #include <QApplication>
  10. #include <QMessageBox>
  11. #include <iostream>
  12. #include <QtDebug>
  13. #include <QSpinBox>
  14. #include <QTimer>
  15. #include "QDebug"
  16. //#include "windows.h"
  17.  
  18. namespace Ui {
  19. class MainWindow;
  20. }
  21.  
  22. class MainWindow : public QMainWindow
  23. {
  24. Q_OBJECT
  25.  
  26. public:
  27. );
  28. ~MainWindow();
  29.  
  30. private slots:
  31. void select();
  32. void on_search_clicked();
  33. void on_addword_clicked();
  34. void on_speak_word_clicked();
  35. void on_del_word_clicked();
  36. void on_back_clicked();
  37. void on_mouse_speak_word_clicked();
  38. void on_revise_word_clicked();
  39. private:
  40. Ui::MainWindow *ui;
  41. QSqlDatabase db; //声明数据库类
  42. QAxObject * voiceObj; //speak
  43.  
  44. };
  45.  
  46. #endif // MAINWINDOW_H

main代码:其中主要是实现了数据库CET的创建,然后又建立了一张CET的数据库表格。程序开启后CET数据库以及CET表格都会自动建立。

  1. #include "mainwindow.h"
  2. #include <QApplication>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. QApplication a(argc, argv);
  7.  
  8. //创建DB文件,创建表格
  9. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  10. std::cout<<"begin sqlite"<<std::endl;
  11. db.setDatabaseName("CET.db");
  12. if ( !db.open()){
  13. QMessageBox::critical(NULL, QObject::tr("Collection"), QObject::tr("failed to connect to database!"));
  14. ;
  15. }
  16. QSqlQuery query;
  17. query.exec("create table CET (id integer PRIMARY KEY AUTOINCREMENT,word text,mean text)");
  18.  
  19. MainWindow w;
  20. w.show();
  21.  
  22. return a.exec();
  23. }

下面介绍主要的代码mainwindow.cpp

先介绍一下怎么实现语音TTS朗读的吧,我认为这个比较好玩。但也是很简单的。其次就是sql数据库的使用了。

TTS实现朗读的代码:

SAPI:

  1. voiceObj = new QAxObject("sapi.spvoice",this);

然后再利用这个代码就搞定了:

  1. voiceObj->dynamicCall()函数。里面的 speak()函数。
  1. voiceObj->dynamicCall("speak(1)", model->data(model->index(i,j)).toString());

一个时间停顿的函数:

头文件:#include“Qtimer.h”

  1. QTime t;
  2. t.start();
  3. ) //时间停顿
  4. {
  5. QCoreApplication::processEvents();
  6. }

对于对数据库表依次的朗读,利用for循环与tableview结合进行数据库的遍历。这里详见另外一篇博客:http://www.cnblogs.com/xiaobo-Linux/p/4684671.html

  1. void MainWindow::on_speak_word_clicked()
  1.  
  1. {
  1. QSqlQueryModel *model = new QSqlQueryModel(ui->tableView);
  1.  
  1. model->setQuery(QString("select *from CET"));
  1.  
  1. //遍历model中的所有数据
  1. for(int i=0;i<model->rowCount();i++) //model->rowCount()获取model的行数
  1. {
  1. for( int times = ui->spReadTimes->value();times>0;times-- ) //读单词次数
  1. {
  1.  
  1. QTime t;
  1. t.start();
  1. while(t.elapsed()< ui->spEveryWord->value()*1000 ) //时间停顿
  1. {
  1. QCoreApplication::processEvents();
  1. }
  1.  
  1.  
  1. for(int j=1;j<model->columnCount();j++) //model->columnCount()获取model的列数
  1. {
  1.  
  1. voiceObj->dynamicCall("speak(1)", model->data(model->index(i,j)).toString());
  1. }
  1. }
  1. }
  1. }

主要的代码:

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent) :
  5. QMainWindow(parent),
  6. ui(new Ui::MainWindow)
  7. {
  8. ui->setupUi(this);
  9. setWindowTitle("小波的CET单词背诵 V1.0");
  10. setWindowIcon(QIcon("xiaobo.ico"));//设置软件图标
  11. select();//显示表格
  12. voiceObj = new QAxObject("sapi.spvoice",this);
  13. }
  14.  
  15. MainWindow::~MainWindow()
  16. {
  17. delete ui;
  18. db.close();
  19. }
  20.  
  21. void MainWindow::on_addword_clicked()
  22. {
  23.  
  24. QSqlQuery query;
  25. query.prepare("insert into CET (id,word,mean)values(1,:word,:mean )"); //id自动增加,先从1起,之后无需手动插入
  26.  
  27. query.prepare("insert into CET (word,mean)values(:word ,:mean )");
  28. query.bindValue(":word",ui->textEdit_1->text());
  29. query.bindValue(":mean",ui->textEdit_2->text());
  30. query.exec();
  31. select();//显示表格
  32. }
  33.  
  34. void MainWindow::on_search_clicked()
  35. {
  36. QSqlQuery query;
  37. ui -> tableView -> clearSpans(); //tableview清空
  38. QSqlQueryModel *model = new QSqlQueryModel(ui->tableView);
  39. query.prepare("select *from CET where word = :word "); // like 模糊查询没成功
  40. query.bindValue(":word",ui->textEdit_3->text());
  41. query.exec();
  42. model->setQuery(query);
  43. model->setHeaderData(,Qt::Horizontal,QObject::tr("顺序"));
  44. model->setHeaderData(,Qt::Horizontal,QObject::tr("单词"));
  45. model->setHeaderData(,Qt::Horizontal,QObject::tr("释义"));
  46. ui->tableView->setModel(model);
  47. }
  48.  
  49. void MainWindow::select()
  50. { //将sql表格显示到tableView里
  51. QSqlQueryModel *model = new QSqlQueryModel(ui->tableView);
  52. model->setQuery(QString("select *from CET"));
  53. model->setHeaderData(,Qt::Horizontal,QObject::tr("顺序"));
  54. model->setHeaderData(,Qt::Horizontal,QObject::tr("单词"));
  55. model->setHeaderData(,Qt::Horizontal,QObject::tr("释义"));
  56. ui->tableView->setModel(model);
  57. }
  58.  
  59. void MainWindow::on_speak_word_clicked()
  60.  
  61. {
  62.  
  63. QSqlQueryModel *model = new QSqlQueryModel(ui->tableView);
  64.  
  65. model->setQuery(QString("select *from CET"));
  66.  
  67. //遍历model中的所有数据
  68. ;i<model->rowCount();i++) //model->rowCount()获取model的行数
  69. {
  70.  
  71. ;times-- ) //读单词次数
  72. {
  73.  
  74. QTime t;
  75. t.start();
  76. ) //时间停顿
  77. {
  78. QCoreApplication::processEvents();
  79. }
  80.  
  81. ;j<model->columnCount();j++) //model->columnCount()获取model的列数
  82. {
  83.  
  84. voiceObj->dynamicCall("speak(1)", model->data(model->index(i,j)).toString());
  85. }
  86. }
  87.  
  88. }
  89.  
  90. }
  91. void MainWindow::on_del_word_clicked()
  92. {
  93.  
  94. QSqlQuery query;
  95.  
  96. int curRow = ui->tableView->currentIndex().row(); //鼠标选择删除第几行
  97.  
  98. QModelIndex index = ui->tableView->currentIndex();
  99.  
  100. ).data().toInt();
  101.  
  102. query.prepare("delete from CET where id = :id");
  103.  
  104. query.bindValue(":id",id);
  105. query.exec();
  106. select();
  107.  
  108. }
  109.  
  110. void MainWindow::on_back_clicked()
  111. {
  112.  
  113. select();
  114. }
  115.  
  116. void MainWindow::on_mouse_speak_word_clicked()
  117. {
  118.  
  119. ;times-- ) //读单词次数
  120. {
  121. voiceObj->dynamicCall("speak(1)",ui->tableView->currentIndex().data()); //鼠标选中tableview单元格内数据来朗读
  122. QTime t;
  123. t.start();
  124. ) //时间停顿
  125. {
  126. QCoreApplication::processEvents();
  127. }
  128. }
  129. }
  130.  
  131. void MainWindow::on_revise_word_clicked()
  132. {
  133. QSqlQuery query;
  134.  
  135. int row = ui->tableView->currentIndex().row(); //鼠标选择第几行
  136.  
  137. QModelIndex index = ui->tableView->currentIndex();
  138.  
  139. ).data().toInt();
  140. query.prepare("update CET set word =:word where id= :id ");
  141. query.bindValue(":id",id);
  142. query.bindValue(":word",ui->textEdit_5->text());
  143. query.exec();
  144. query.prepare("update CET set mean =:mean where id= :id ");
  145. query.bindValue(":id",id);
  146. query.bindValue(":mean",ui->textEdit_4->text());
  147. query.exec();
  148.  
  149. select();
  150.  
  151. }

mainwindow。ui的界面设计:

代码的演示部分:

视频演示:

http://files.cnblogs.com/files/xiaobo-Linux/%E5%B0%8F%E6%B3%A2%E7%9A%84CET%E5%8D%95%E8%AF%8D%E8%83%8C%E8%AF%B5%E8%BD%AF%E4%BB%B6%E8%A7%86%E9%A2%91%E6%BC%94%E7%A4%BA.zip

软件的发布版本:http://pan.baidu.com/s/1mgrIMwC

基于Qt5.5.0的sql数据库、SDK_tts文本语音朗读的CET四六级单词背诵系统软件的编写V1.0的更多相关文章

  1. 【工具篇】利用DBExportDoc V1.0 For MySQL自动生成数据库表结构文档

    对于DBA或开发来说,如何规范化你的数据库表结构文档是灰常之重要的一件事情.但是当你的库,你的表排山倒海滴多的时候,你就会很头疼了. 推荐一款工具DBExportDoc V1.0 For MySQL( ...

  2. 清除SQL数据库文本字段中的回车、换行符的方法

    清除SQL数据库中文本字段的回车.换行符的方法 清除回车符: update tableName set columnName = rtrim(ltrim(replace(columnName ,cha ...

  3. 【Beta】“北航社团帮”测试报告——小程序v2.0与网页端v1.0

    目录 测试计划.过程和结果 后端测试--单元测试与覆盖率 后端测试--压力测试 展示部分数据 平均数据 前端测试--小程序v2.0 授权登录与权限检查 新功能的测试 兼容性测试 性能测试 前端测试-- ...

  4. 后台框架 FastAdmin V1.0.0.20200228 发布,为疫情防控作贡献

    后台框架 FastAdmin V1.0.0.20200228 发布,为疫情防控作贡献 https://www.oschina.net/news/113694/fastadmin-1-20200228- ...

  5. J20航模遥控器开源项目系列教程(一)制作教程 | 基础版V1.0发布,从0到1

    我们的开源宗旨:自由 协调 开放 合作 共享 拥抱开源,丰富国内开源生态,开展多人运动,欢迎加入我们哈~ 和一群志同道合的人,做自己所热爱的事! 项目开源地址:https://github.com/C ...

  6. 基于Qt5.5.0的sql,C++备忘录软件的编写

    我的第一个软件. 基于Qt5.5.0的 sql ,C++备忘录软件version1.0的编写 我用的Qt版本是5.5.0免配置编译器的版本,这里附上我使用的软件下载地址:http://download ...

  7. 【C#】SQL数据库助手类2.0(自用)

    using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...

  8. 跨服务器备注SQL数据库 分类: SQL Server 2015-03-05 08:52 227人阅读 评论(0) 收藏

    任务:把服务器1上的SQL数据库自动备份到服务器2上,命名格式=数据库名+年月日+小时. 说明: 服务器2=>192.168.0.22 数据库名=>Book 共享文件夹路径:192.168 ...

  9. Microsoft-PetSop4.0(宠物商店)-数据库设计-Sql

    ylbtech-DatabaseDesgin:Microsoft-PetSop4.0(宠物商店)-数据库设计-Sql DatabaseName:PetShop(宠物商店) Model:宠物商店网站 T ...

随机推荐

  1. Java经典实例:把字符串解析为日期时间

    Java版本:1.8开始 import java.time.LocalDate; import java.time.LocalDateTime; /** * Created by Frank */ p ...

  2. Ionic 今天发布了Windows 桌面版的IDE Ionic Lab

    Ionic简介: Ionic 是一个强大的 HTML5 应用程序开发框架,号称 Advanced HTML5 Hybrid Mobile AppFramework 是 AngularJS 移动端解决方 ...

  3. Smint – 用于单页网站制作的 jQuery 导航菜单插件

    Smint 是一款用于实现单页风格网站的 jQuery 导航插件,包含两部分:固定在页面顶部的精美导航条和能够在你点击的时候自动滚动到对应内容的菜单按钮.Smint 使用非常简单,只有一个参数用于设置 ...

  4. Webpack - CommonJs & AMD 模块打包器

    Webpack 是一个 CommonJs & AMD 模块打包器.可以把你的 JavaScript 代码分离为多个包,在需要的时候进行加载,支持预处理文件,例如 json, jade, cof ...

  5. 【精心推荐】几款实用的 JavaScript 图形图表库

    一款好的图表插件不是那么容易找到的.最近项目里需要实现统计图表功能,所以在网上搜罗了一圈,找到一些不错的图表插件,分享大家.众多周知,图形和图表要比文本更具表现力和说服力.这里给大家精心推荐几款实用的 ...

  6. Treed – 基于拖放 操作的,强大的树形编辑器

    Treed 是一个功能强大的树型编辑组件.Treed 使用 MVC 模式,简单的构造可以帮助你轻松创建一个完全不同的树形视图.您也可以创建自己的“节点”类,如果你想要做的不仅仅是单一的文本输入. 您可 ...

  7. android 图片加载库 Glide 的使用介绍

    一:简介 在泰国举行的谷歌开发者论坛上,谷歌为我们介绍了一个名叫 Glide 的图片加载库,作者是bumptech.这个库被广泛的运用在google的开源项目中,包括2014年google I/O大会 ...

  8. UITableView

    知识点: 1)UITableView 2)UITableViewCell ====================================================== 一.UITabl ...

  9. iOS设置文字过长时的显示格式

    以label为例: //设置文字过长时的显示格式 aLabel.lineBreakMode = UILineBreakModeMiddleTruncation; //截去中间 aLabel.lineB ...

  10. 使用mac 终端利用alias设置快捷命令

    在终端中输入快捷命令可以提高工作效率,同时可以少记很多命令 如何做: 首先在~/目录下编辑 .bash_profile这个隐藏文件,如果你想直接双击此文件打开编辑的话请在终端输入 Mac 显示隐藏文件 ...