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

简介: 作者:小波 QQ:463431476 请关注我的博客园:http://www.cnblogs.com/xiaobo-Linux/  我的第二款软件:CET四六级单词背诵软件。基于QT5.5.0、sql数据库、以及TTS文本识别语音单词朗读的一款软件。

作者:小波

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图标文件必须的。

#-------------------------------------------------
#
# Project created by QtCreator 2015-07-28T13:25:29
#
#-------------------------------------------------

QT       += core gui axcontainer #axcontainer speak
QT += sql         #sql

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = CET_words
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui


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语音库的接口。

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QAxObject> //speak com
#include <QSqlDatabase> //数据库类
#include <QSqlQuery>    //执行语句类
#include <QSqlRecord>   //返回记录类
#include <QtSql>
#include <QApplication>
#include <QMessageBox>
#include <iostream>
#include <QtDebug>
#include <QSpinBox>
#include <QTimer>
#include "QDebug"
//#include "windows.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();



private slots:
   void select();
   void on_search_clicked();
   void on_addword_clicked();
   void on_speak_word_clicked();
   void on_del_word_clicked();
   void on_back_clicked();
   void on_mouse_speak_word_clicked();
   void  on_revise_word_clicked();
private:
    Ui::MainWindow *ui;
    QSqlDatabase db;      //声明数据库类
    QAxObject * voiceObj; //speak


};

#endif // MAINWINDOW_H

 

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

#include "mainwindow.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("CET.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 CET (id integer PRIMARY KEY AUTOINCREMENT,word text,mean text)");

    MainWindow w;
    w.show();

    return a.exec();
}

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

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

TTS实现朗读的代码:

SAPI:

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

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

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


一个时间停顿的函数:

头文件:#include“Qtimer.h”

             QTime t;
             t.start();
             while(t.elapsed()< ui->spEveryWord->value()*1000 ) //时间停顿
             {
             QCoreApplication::processEvents();
             }

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

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


主要的代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("小波的CET单词背诵 V1.0");
    setWindowIcon(QIcon("xiaobo.ico"));//设置软件图标
    select();//显示表格
    voiceObj = new QAxObject("sapi.spvoice",this);
}

MainWindow::~MainWindow()
{
    delete ui;
    db.close();
}


void MainWindow::on_addword_clicked()
{

    QSqlQuery query;
    query.prepare("insert into CET (id,word,mean)values(1,:word,:mean )"); //id自动增加,先从1起,之后无需手动插入

    query.prepare("insert into CET (word,mean)values(:word ,:mean )");
    query.bindValue(":word",ui->textEdit_1->text());
    query.bindValue(":mean",ui->textEdit_2->text());
    query.exec();
    select();//显示表格
}

void MainWindow::on_search_clicked()
{
         QSqlQuery query;
         ui -> tableView -> clearSpans(); //tableview清空
         QSqlQueryModel *model = new QSqlQueryModel(ui->tableView);
         query.prepare("select  *from CET where word = :word ");  // like 模糊查询没成功
         query.bindValue(":word",ui->textEdit_3->text());
         query.exec();
          model->setQuery(query);
          model->setHeaderData(0,Qt::Horizontal,QObject::tr("顺序"));
          model->setHeaderData(1,Qt::Horizontal,QObject::tr("单词"));
          model->setHeaderData(2,Qt::Horizontal,QObject::tr("释义"));
          ui->tableView->setModel(model);
}



void MainWindow::select()
{  //将sql表格显示到tableView里
 QSqlQueryModel *model = new QSqlQueryModel(ui->tableView);
 model->setQuery(QString("select *from CET"));
 model->setHeaderData(0,Qt::Horizontal,QObject::tr("顺序"));
 model->setHeaderData(1,Qt::Horizontal,QObject::tr("单词"));
 model->setHeaderData(2,Qt::Horizontal,QObject::tr("释义"));
 ui->tableView->setModel(model);
}

void MainWindow::on_speak_word_clicked()

{

     QSqlQueryModel *model = new QSqlQueryModel(ui->tableView);

     model->setQuery(QString("select *from CET"));


  //遍历model中的所有数据
     for(int i=0;i<model->rowCount();i++)       //model->rowCount()获取model的行数
    {


         for( int times = ui->spReadTimes->value();times>0;times-- ) //读单词次数
          {

             QTime t;
             t.start();
             while(t.elapsed()< ui->spEveryWord->value()*1000 ) //时间停顿
             {
             QCoreApplication::processEvents();
             }


        for(int j=1;j<model->columnCount();j++)       //model->columnCount()获取model的列数
        {

              voiceObj->dynamicCall("speak(1)", model->data(model->index(i,j)).toString());
          }
    }

}


}
void MainWindow::on_del_word_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 CET where id = :id");

    query.bindValue(":id",id);
    query.exec();
    select();

}

void MainWindow::on_back_clicked()
{

    select();
}

void MainWindow::on_mouse_speak_word_clicked()
{

    for( int times = ui->spReadTimes->value();times>0;times-- ) //读单词次数
     {
    voiceObj->dynamicCall("speak(1)",ui->tableView->currentIndex().data()); //鼠标选中tableview单元格内数据来朗读
    QTime t;
    t.start();
    while(t.elapsed()< ui->spEveryWord->value()*1000 ) //时间停顿
    {
    QCoreApplication::processEvents();
    }
    }
}


void MainWindow::on_revise_word_clicked()
{
    QSqlQuery query;

    int row = ui->tableView->currentIndex().row();     //鼠标选择第几行

     QModelIndex index = ui->tableView->currentIndex();

     int id = index.sibling(row,0).data().toInt();
     query.prepare("update CET set word =:word where id= :id ");
     query.bindValue(":id",id);
     query.bindValue(":word",ui->textEdit_5->text());
      query.exec();
      query.prepare("update CET set mean =:mean where id= :id ");
      query.bindValue(":id",id);
      query.bindValue(":mean",ui->textEdit_4->text());
      query.exec();

      select();



}

 

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

目录
相关文章
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
2006 152
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
1401 156
|
SQL 存储 关系型数据库
第二篇:关系型数据库的核心概念与 SQL 基础
本篇内容深入浅出地讲解了关系型数据库的核心概念与SQL基础,适合有一定计算机基础的学习者。文章涵盖数据库的基本操作(CRUD)、数据类型、表的创建与管理等内容,并通过实例解析SELECT、INSERT、UPDATE、DELETE等语句的用法。此外,还推荐了多种学习资源与实践建议,帮助读者巩固知识。学完后,你将掌握基础数据库操作,为后续高级学习铺平道路。
973 1
|
SQL 数据库 数据安全/隐私保护
数据库数据恢复——sql server数据库被加密的数据恢复案例
SQL server数据库数据故障: SQL server数据库被加密,无法使用。 数据库MDF、LDF、log日志文件名字被篡改。 数据库备份被加密,文件名字被篡改。
|
SQL 关系型数据库 MySQL
Go语言数据库编程:使用 `database/sql` 与 MySQL/PostgreSQL
Go语言通过`database/sql`标准库提供统一数据库操作接口,支持MySQL、PostgreSQL等多种数据库。本文介绍了驱动安装、连接数据库、基本增删改查操作、预处理语句、事务处理及错误管理等内容,涵盖实际开发中常用的技巧与注意事项,适合快速掌握Go语言数据库编程基础。
1829 213
|
SQL 人工智能 Linux
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
1000 5
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
|
SQL 存储 监控
SQL日志优化策略:提升数据库日志记录效率
通过以上方法结合起来运行调整方案, 可以显著地提升SQL环境下面向各种搜索引擎服务平台所需要满足标准条件下之数据库登记作业流程综合表现; 同时还能确保系统稳健运行并满越用户体验预期目标.
595 6
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
SQL Oracle 关系型数据库
Oracle数据库创建表空间和索引的SQL语法示例
以上SQL语法提供了一种标准方式去组织Oracle数据库内部结构,并且通过合理使用可以显著改善查询速度及整体性能。需要注意,在实际应用过程当中应该根据具体业务需求、系统资源状况以及预期目标去合理规划并调整参数设置以达到最佳效果。
838 8
|
SQL 人工智能 Java
用 LangChain4j+Ollama 打造 Text-to-SQL AI Agent,数据库想问就问
本文介绍了如何利用AI技术简化SQL查询操作,让不懂技术的用户也能轻松从数据库中获取信息。通过本地部署PostgreSQL数据库和Ollama模型,结合Java代码,实现将自然语言问题自动转换为SQL查询,并将结果以易懂的方式呈现。整个流程简单直观,适合初学者动手实践,同时也展示了AI在数据查询中的潜力与局限。
1633 8

推荐镜像

更多
  • qt