點名系統
這是程序運行圖,包含點名系統,語音播報功能,以及記錄存到數據庫等;
#include "app.h"
#include "ui_app.h"
#include <QFileDialog>
#include <QFile>
#include <QDebug>
#include <QProcess>
#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlQuery>
#include <QString>
#include <QMediaPlayer>
app::app(QWidget *parent) :
QWidget(parent),
ui(new Ui::app)
{
ui->setupUi(this);
this->setWindowTitle("CallName 1.0");
this->setWindowIcon(QIcon(":/icons/png-0628.png"));
db = QSqlDatabase::addDatabase("QSQLITE");
ui->pushButton_CA->setEnabled(false);
ui->pushButton_AB->setEnabled(false);
ui->pushButton_AG->setEnabled(false);
ui->pushButton_begin->setEnabled(false);
//ui->pushButton_end->setEnabled(false);
ui->pushButton_begin_2->setEnabled(false);
}
app::~app()
{
delete ui;
}
void app::spake(QString &namep)
{
// QString file_mp3 = tr("name/%0.mp3").arg(namep);
// QFileInfo info(file_mp3);
// if(!info.exists())
// {
// char command[256] = {0};
// sprintf(command,"ttsmain %s",namep.toUtf8().data());
// qDebug()<<"command :"<<command;
// system(command);
// memset(command,0,256);
// sprintf(command,"move result.mp3 name/%s.mp3",namep.toLocal8Bit().data());
// system(command);
// }
// char command[256] = {0};
// sprintf(command,"../CallName/ttsmain %s",namep.toUtf8().data());
// qDebug()<<"command :"<<command;
// system(command);
// system("play result.mp3");
// this->play = new QMediaPlayer;
// QString localfile = tr("name/%0.mp3").arg(namep);
// play->setMedia(QUrl::fromLocalFile(localfile));
// play->setVolume(50);
// play->play();
QString file_mp3 = tr("name/%0.mp3").arg(name);
qDebug()<<"file_mp3"<<file_mp3 ;
QFileInfo info(file_mp3);
qDebug()<<"file is exist:"<< info.exists();
if(!info.exists())
{
QFile file("tts/tmp");
if(file.open(QIODevice::ReadWrite|QIODevice::Truncate|QIODevice::Text))
{
file.write(name.toUtf8());
file.close();
}
char command[256] = {0};
// sprintf(command,"cd tts&&ttsmain_female.exe");
qDebug()<<"command1:"<<command ;
system("cd tts && ttsmain_female.exe");
memset(command,0,256);
sprintf(command,"cd tts && move result.mp3 ../name/%s.mp3",name.toLocal8Bit().data());
system(command);
// sprintf(command,"rename names/result.mp3 names/%s.mp3",name.toUtf8().data());
// sprintf(command,"move result.mp3 names/%s.mp3",name.toUtf8().data());
// qDebug()<<"command2:"<<command ;
// system(command);
}
// system("play result.mp3");
// QString program = "../CallName/ttsmain";
// program += ' ';
// program += name ;
// qDebug()<<program ;
// QProcess * myProcess = new QProcess(this);
// myProcess->start(program);
this->play = new QMediaPlayer ;
QString localfile = tr("name/%0.mp3").arg(name);
play->setMedia(QUrl::fromLocalFile(localfile));
play->setVolume(50);
play->play();
}
void app::on_pushButton_class_clicked()
{
filename = QFileDialog::getOpenFileName(this,tr("Open File"),tr("./"),tr("*.*"));
QFileInfo info(filename);
ui->lineEdit_class->setText(info.fileName());
ui->pushButton_begin->setEnabled(true);
}
void app::on_pushButton_begin_clicked()
{
ui->pushButton_CA->setEnabled(true);
ui->pushButton_AB->setEnabled(true);
ui->pushButton_AG->setEnabled(true);
qDebug()<<"1"<<x;
db.setDatabaseName(class1);
if(!db.open())
{
QMessageBox::critical(this,tr("Error"),tr("open the db error"));
}
ui->pushButton_begin->setEnabled(false);
if(!filename.isEmpty())
{
filp = new QFile(filename);
filp->open(QIODevice::ReadOnly|QIODevice::Text);
input = new QTextStream(filp);
name = input->readLine(1024);
qDebug()<<"name is"<<name;
ui->lineEdit_name->setText(name);
}
spake(name);
}
void app::on_pushButton_CA_clicked()
{
QString sql = tr("select * from %0 where name='%1';").arg(x).arg(name) ;
QSqlQuery query;
qDebug()<<"sql:"<<sql;
if (!query.exec(sql))
{
QMessageBox::warning(NULL, "Error",tr("select failed"));
}
if(query.next())// cunzai
{
int chu = query.value("chu").toInt() + 1;
sql = tr("update %0 set chu=%1 where name='%2';").arg(x).arg(chu).arg(name) ;
qDebug()<<"sql:"<<sql;
if (! query.exec(sql))
{
QMessageBox::warning(NULL, "class1.db",tr("update failed"));
}
}
else // bucunzai
{
int chu = 1;
int que = 0 ;
sql = tr("insert into %0 values('%1',%2,%3) ;").arg(x).arg(name).arg(chu).arg(que) ;
qDebug()<<"sql:"<<sql;
if (! query.exec(sql))
{
QMessageBox::warning(NULL, "class.db",
tr("update failed"));
}
}
name+=" is turn out for work";
ui->label_3->setText(name);
name = input->readLine(1024);
ui->lineEdit_name->setText(name);
if(name == "點名結束")
{
ui->pushButton_CA->setEnabled(false);
ui->pushButton_AB->setEnabled(false);
ui->pushButton_AG->setEnabled(false);
}
spake(name);
}
void app::on_pushButton_AB_clicked()
{
QString sql = tr("select * from %0 where name='%1';").arg(x).arg(name) ;
QSqlQuery query;
if (! query.exec(sql))
{
QMessageBox::warning(NULL, "Error",tr("select failed"));
}
if(query.next())// cunzai
{
int que = query.value("que").toInt() + 1;
sql = tr("update %0 set que=%1 where name='%2';").arg(x).arg(que).arg(name) ;
if (! query.exec(sql))
{
QMessageBox::warning(NULL, "class1.db",tr("update failed"));
}
}
else // bucunzai
{
int chu = 0;
int que = 1 ;
sql = tr("insert into %0 values('%1',%2,%3) ;").arg(x).arg(name).arg(chu).arg(que) ;
qDebug()<<"sql:"<<sql;
if (! query.exec(sql))
{
QMessageBox::warning(NULL, "class.db",
tr("update failed"));
}
}
name+=" is absence from duty";
ui->label_3->setText(name);
name = input->readLine(1024);
ui->lineEdit_name->setText(name);
if(name == "點名結束")
{
ui->pushButton_CA->setEnabled(false);
ui->pushButton_AB->setEnabled(false);
ui->pushButton_AG->setEnabled(false);
}
spake(name);
}
void app::on_pushButton_end_clicked()
{
ui->pushButton_begin->setEnabled(true);
ui->pushButton_CA->setEnabled(false);
ui->pushButton_AB->setEnabled(false);
ui->pushButton_AG->setEnabled(false);
}
void app::on_pushButton_AG_clicked()
{
spake(name);
}
void app::on_pushButton_begin_2_clicked()
{
char a[256] = {0};
QString c;
db.setDatabaseName(class1);
if(!db.open())
{
QMessageBox::critical(this,tr("Error"),tr("open the db error"));
}
QString sql = tr("select * from %0;").arg(x);
QSqlQuery query;
if (! query.exec(sql))
{
QMessageBox::warning(NULL, "Error",tr("select failed"));
}
while(query.next())
{
sprintf(a,"name:%12s Present :%5d Absent :%5d\n",query.value(0).toString().toUtf8().data(),
query.value(1).toInt(),
query.value(2).toInt());
c+=a;
}
QMessageBox msg;
msg.setWindowTitle(tr("Consolidated Statement"));
msg.setWindowIcon(QIcon(":/icons/png-0628.png"));
msg.setText(c);
msg.exec();
}
void app::on_pushButton_database_clicked()
{
class1 = QFileDialog::getOpenFileName(this,tr("Open File"),tr("./"),tr("*.*"));
QFileInfo info(class1);
ui->lineEdit_class_2->setText(info.fileName());
// int index;
// index = info.fileName().lastIndexOf(".");
// index = fileName.lastIndexOf("."); //一般來說,擴展名爲最後一個“.”之後的字符;
// fileName.truncate(index);//把它cate掉
// x = tr(info.fileName().truncate(index));
x = info.baseName();
ui->pushButton_begin_2->setEnabled(true);
//qDebug()<<"1"<<x;
}
void app::on_pushButton_2_clicked()
{
QString sql = tr("delete from %0;").arg(x);
QSqlQuery query;
query.exec(sql);
}
void app::on_pushButton_clicked()
{
QMessageBox msg ;
msg.setWindowTitle(tr("Help"));
msg.setWindowIcon(QIcon(":/icons/png-0628.png"));
filp_1 = new QFile ("help.txt");
filp_1->open(QIODevice::ReadWrite|QIODevice::Text);
QByteArray data = filp_1->readAll();
msg.setText(data);
msg.exec();
filp_1->close();
// filp = new QFile(filename);
// filp->open(QIODevice::ReadOnly|QIODevice::Text);
// input = new QTextStream(filp);
// name = input->readLine(1024);
// qDebug()<<"name is"<<name;
// ui->lineEdit_name->setText(name);
}
其中不包含.h程序,可以參考代碼適當的在頭文件加一些函數的聲明;
其中數據庫不是使用的window中數據庫,數據庫是從Ubuntu中移植過來;