.cpp
#include "appp.h"
#include "ui_appp.h"
#include <QDebug>
#include <QMessageBox>
#include <QStandardItemModel>
#include <QMediaPlayer>
#include <QSqlDatabase>
#include <QSqlQuery>
void speack(QString &name);
appp::appp(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::appp)
{
ui->setupUi(this);
this->setWindowTitle(tr("Order v1.0"));
model = new QStandardItemModel(this);
/*设置列字段名*/
model->setColumnCount(1);
model->setHeaderData(0,Qt::Horizontal, "姓名");
ui->queqinmingdan->setModel(model);
//插入背景图片
QPixmap pixmap(":/111.jpg");
QPalette palette=this->palette();
palette.setBrush(QPalette::Background,QBrush(pixmap));
this->setPalette(palette);
db = QSqlDatabase::addDatabase("QSQLITE");
}
appp::~appp()
{
delete ui;
}
void appp::speack(QString &name)
{
QString file_mp3 = tr("names/%0.mp3").arg(name);
QFileInfo info(file_mp3);
qDebug()<<info.exists();
if (! info.exists())
{
char commond[1024] = {0};
sprintf(commond,"./ttsmain %s",name.toUtf8().data());
system(commond);
memset(commond,0,1024);
sprintf(commond,"mv result.mp3 names/%s.mp3",name.toUtf8().data());
system(commond);
}
//system("play result.mp3");
this->play = new QMediaPlayer ;
QString file_path = tr("/home/linux/mywork/qt_home/qt_build/names/%0.mp3").arg(name);
qDebug()<<file_path;
play->setMedia(QUrl::fromLocalFile(file_path));
play->setVolume(50);
play->play();
}
//xuan ze ban ji
void appp::on_pushButton_selectfile_clicked()
{
file_name = QFileDialog::getOpenFileName(this,tr("Open"),"../",tr("*.*"));
qDebug()<<file_name;
QFileInfo info(file_name);
QString banji = info.fileName();
ui->lineEdit_file->setText(banji);
}
// start
void appp::on_pushButton_start_clicked()
{
if (file_name == "")
{
return ;
}
i_shi = 0 ;
i_que = 0 ;
file= new QFile(file_name);
file->open(QIODevice::ReadOnly |QIODevice::Text );
input = new QTextStream(file);
QString name ;
name = input->readLine(1024);
total++;
ui->lineEdit_name->setText(name);
ui->pushButton_start->setEnabled(false);
// char commond[1024] = {0};
// sprintf(commond,"../OrderName/ttsmain %s","同学们请安静一下");
// system(commond);
// system("play result.mp3");
db.setDatabaseName("/home/linux/mywork/qt_db/class.db");
if (!db.open())
{
QMessageBox::information(this,tr("information"),tr("数据库打开失败!"),QMessageBox::Ok,QMessageBox::Ok);
return ;
}
speack(name);
}
// chu qin
void appp::on_pushButton_dao_clicked()
{
if (file == NULL)
{
return;
}
QString name;
if ((total - i_que)>i_shi)
{
name = ui->lineEdit_name->text();
QString sql = tr("select * from class where name ='%0';").arg(name);
qDebug()<<"sql"<<sql;
QSqlQuery query;
if (!query.exec(sql))
{
QMessageBox::warning(NULL,"class db",tr("select failed"));
}
if (query.next())
{
int chu = query.value("dao").toInt()+1;
sql = tr("update class set dao = %0 where name ='%1';").arg(chu).arg(name);
if (!query.exec(sql))
{
QMessageBox::warning(NULL,"class db",tr("update failed"));
}
}
else
{
int chu = 1 ;
int que = 0 ;
sql = tr("insert into class values('%0',%1,%2);").arg(name).arg(chu).arg(que);
if (! query.exec(sql))
{
QMessageBox::warning(NULL,"class db",tr("insert failed"));
}
}
i_shi ++;
name +="已到";
speack(name);
}
}
// que qin
void appp::on_pushButton_que_clicked()
{
if (file == NULL)
{
return ;
}
QString name;
if ((total - i_shi)>i_que)
{
name = ui->lineEdit_name->text();
model->setItem(i_que, 0, new QStandardItem(name));
QString sql = tr("select * from class where name ='%0';").arg(name);
qDebug()<<"sql"<<sql;
QSqlQuery query;
if (!query.exec(sql))
{
QMessageBox::warning(NULL,"class db",tr("select failed"));
}
if (query.next()) //cunzai
{
int que = query.value("que").toInt()+1;
sql = tr("update class set dao = %0 where name ='%1';").arg(que).arg(name);
if (!query.exec(sql))
{
QMessageBox::warning(NULL,"class db",tr("update failed"));
}
}
else
{
int chu = 0 ;
int que = 1 ;
sql = tr("insert into class values('%0',%1,%2);").arg(name).arg(chu).arg(que);
if (! query.exec(sql))
{
QMessageBox::warning(NULL,"class db",tr("insert failed"));
}
}
i_que ++;
name += "缺勤";
speack(name);
}
}
// chong fu
void appp::on_pushButton_again_clicked()
{
if (file == NULL)
{
return ;
}
//ting zai zhe ge di fang zhi shi huo qu mingi zai jiao yi ci
QString str = ui->lineEdit_name->text();
speack(str);
}
// jie shu
void appp::on_pushButton_over_clicked()
{
if (file != NULL)
file->close();
file = NULL ;
ui->pushButton_start->setEnabled(true);
ui->lineEdit_file->clear();
ui->lineEdit_name->clear();
input->flush();
model->clear();
db.close();
file_name.clear();
}
void appp::on_pushButton_order_clicked()
{
if (file == NULL)
{
return ;
}
QString name;
if ((total)==(i_shi + i_que))
{
name = input->readLine(1024);
total++;
if (name == "")
{
QMessageBox::information(this,tr("infomation"),tr("本次点名结束!\n"
"总人数 :[%1] \n"
"实到人数:[%2]\n"
"缺勤人数:[%3]").arg(i_shi + i_que ).arg(i_shi ).arg(i_que),QMessageBox::Ok , QMessageBox::Ok);
ui->pushButton_start->setEnabled(true);
file->close();
file = NULL ;
QString str = "本次点名结束";
speack(str);
}
else
{
ui->lineEdit_name->setText(name);
speack(name);
}
}
}
.h
#ifndef APPP_H
#define APPP_H
#include <QMainWindow>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QTextStream>
#include <QStandardItemModel>
#include <QMediaPlayer>
#include <QSqlDatabase>
namespace Ui {
class appp;
}
class appp : public QMainWindow
{
Q_OBJECT
public:
explicit appp(QWidget *parent = 0);
~appp();
private slots:
void on_pushButton_selectfile_clicked();
void on_pushButton_start_clicked();
void on_pushButton_dao_clicked();
void on_pushButton_que_clicked();
void on_pushButton_again_clicked();
void on_pushButton_over_clicked();
void speack(QString &name);
void on_pushButton_order_clicked();
private:
Ui::appp *ui;
QString file_name;
QStandardItemModel* model;
QString name_tmp;
QFile *file = NULL;
QTextStream * input;
QMediaPlayer *play;
QSqlDatabase db;
int i_shi = 0 ;
int i_que = 0 ;
int total = 0 ;
};
#endif // APPP_H
main.cpp
#include "appp.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
appp w;
w.show();
return a.exec();
}