思维导图
练习题
1>简单实现闹钟播报,设置时间,当系统时间与设置时间相同时播报语音5次,然后停止。如果设置时间小于当前系统时间,则弹出消息提示框,并清空输入框。
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);ui->timeLabel->setAlignment(Qt::AlignCenter);ui->showLabel->setAlignment(Qt::AlignCenter);id = startTimer(1000);}
int flag = 0;//在点击按钮才会有语音播报
QString time_str;//时间字符串
void Widget::timerEvent(QTimerEvent *event)
{//显示系统时间QTime sys_time = QTime::currentTime();time_str = sys_time.toString("hh:mm:ss");ui->timeLabel->setText(time_str);//获取输入的时间,和系统时间比较QString setTime = ui->lineEdit->text();//时间到则语音播报label文本内容QTextToSpeech *tts = new QTextToSpeech(this);if(setTime == time_str && flag == 1){for(int i = 1; i <= 5; i++){tts->say(ui->showLabel->text());}ui->showLabel->setText("播报完毕,关闭闹钟");killTimer(id);}
}Widget::~Widget()
{delete ui;
}void Widget::on_timeBtn_clicked()
{if(ui->lineEdit->text() < time_str){//设定时间小于当前时间int ret = QMessageBox::information(this,"提示","设定时间小于当前时间,请重新设定!",QMessageBox::Ok);if(ret == QMessageBox::Ok){//确认点击ok后,清空输入框ui->lineEdit->setText("");}}else{flag = 1;ui->showLabel->setText("时间到了");}}