#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
, speecher(new QTextToSpeech(this))
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId() == id)
{
QTime sys_time = QTime::currentTime();
QString s = sys_time.toString("hh:mm:ss");
ui->label->setText(s);
}
else
{
QTime sys_time =QTime::currentTime();
//将系统时间转换成字符串
QString s1 = sys_time.toString("hh:mm:ss");
if(ui->lineEdit->text() == s1)
{
speecher->say(ui->label_4->text());
}
else
{
//关闭一个定时器
killTimer(id2);
//将按钮上的文本设置成启动
ui->pushButton_2->setText("启动闹钟");
}
}
}
void Widget::keyPressEvent(QKeyEvent *event)
{
ui->label_2->setText(event->text()+"按下");
switch (event->key())
{
case 'W':
{
ui->label_2->move(ui->label_2->x(),ui->label_2->y()-5);
if(ui->label_2->y()<0-ui->label_2->height())
{
ui->label_2->move(ui->label_2->x(),this->height());
}
} break;
case 'A':
{
ui->label_2->move(ui->label_2->x()-5,ui->label_2->y());
if(ui->label_2->x()<0-ui->label_2->width())
{
ui->label_2->move(this->width(),ui->label_2->y());
}
} break;
case 'S':
{
ui->label_2->move(ui->label_2->x(),ui->label_2->y()+5);
if(ui->label_2->y()>this->height()-ui->label_2->width())
{
ui->label_2->move(ui->label_2->x(),0);
}
} break;
case 'D':
{
ui->label_2->move(ui->label_2->x(),ui->label_2->y()-5);
if(ui->label_2->y()<0-ui->label_2->height())
{
ui->label_2->move(ui->label_2->x(),this->height());
}
}break;
}
}
void Widget::keyReleaseEvent(QKeyEvent *event)
{
ui->label_2->setText(event->text()+"抬起");
}
void Widget::on_pushButton_clicked()
{
if(ui->pushButton->text() == "启动")
{
//启动一个定时器
id = startTimer(1000); //让系统每隔1秒钟 执行timerEvent()函数
//将按钮上的文本设置成关闭
ui->pushButton->setText("关闭");
}
else
{
//关闭一个定时器
killTimer(id);
//将按钮上的文本设置成启动
ui->pushButton->setText("启动");
}
}
void Widget::on_pushButton_2_clicked()
{
if(ui->pushButton_2->text() == "启动闹钟")
{
//启动一个定时器
id2 = startTimer(1000); //让系统每隔1秒钟 执行timerEvent()函数
}
else
{
//关闭一个定时器
killTimer(id2);
//将按钮上的文本设置成启动
ui->pushButton_2->setText("启动闹钟");
}
}
4.10作业
最新推荐文章于 2024-08-31 22:41:09 发布