2023/4/8 时钟练习
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
t = new QTimer(this);
broadCaster = new QTextToSpeech(this);
get_time();
initCurrentTime();
connect(t,&QTimer::timeout,this,&Widget::updateClock);
}
Widget::~Widget()
{
delete ui;
}
void Widget::initCurrentTime()
{
connect(t,&QTimer::timeout,this,&Widget::showCurrentTime);
t->start(1000);
showCurrentTime();
}
void Widget::showCurrentTime()
{
QString date = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
ui->current_Time->setText(date);
ui->current_Time->setAlignment(Qt::AlignCenter);
}
void Widget::get_time()
{
QString curr_time = QTime::currentTime().toString("hh:mm:ss");
qDebug()<<curr_time;
QStringList current_List = curr_time.split(":");
hour_A = current_List[0].toUInt();
min_A = current_List[1].toUInt();
sec_A = current_List[2].toUInt();
qDebug()<<hour_A << " " << min_A << " " << sec_A;
}
void Widget::on_startBtn_clicked()
{
if(ui->startBtn->text() == "启动")
{
t_id = this->startTimer(1000);
ui->startBtn->setDisabled(true);
ui->stopBtn->setDisabled(false);
}
}
void Widget::on_stopBtn_clicked()
{
if(ui->stopBtn->text() == "关闭")
{
if(t_id != 0)
{
this->killTimer(t_id);
}
ui->startBtn->setDisabled(false);
ui->stopBtn->setDisabled(true);
}
}
void Widget::timerEvent(QTimerEvent *event)
{
if(event->timerId() == t_id)
{
QString time = QTime::currentTime().toString("hh:mm:ss");
QStringList list = time.split(":");
int hh,mm,ss;
hh = list[0].toUInt();
mm = list[1].toUInt();
ss = list[2].toUInt();
int h,m,s;
QStringList list_clock = ui->set_alarm->text().split(":");
h = list_clock[0].toUInt();
m = list_clock[1].toUInt();
s = list_clock[2].toUInt();
qDebug()<< h << " " << m << " " << s;
if(h == hh && m == mm && s == ss)
{
broadCaster->say(ui->broadcast->toPlainText());
qDebug()<<"成功";
this->killTimer(t_id);
ui->startBtn->setDisabled(false);
ui->stopBtn->setDisabled(true);
}
}
}
void Widget::mousePressEvent(QMouseEvent *event)
{
startPoint = event->globalPos() - this->frameGeometry().topLeft();
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
this->move(event->globalPos() - startPoint);
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter p(this);
p.setPen(QColor(85,170,255));
p.setFont(QFont("宋体"));
p.translate(this->width()/4,this->height()/2+30);
p.drawEllipse(QPointF(0,0),100,100);
p.setPen(QColor(255,0,255));
for(int i=1;i<=60;i++)
{
QPen pen;
p.rotate(6);
if(i%5==0)
{
pen.setWidth(4);
p.drawLine(0,-100,0,-80);
p.drawText(0,-80,QString("%1").arg(i/5));
}else
{
pen.setWidth(1);
p.drawLine(0,-100,0,-95);
}
}
p.setPen(QPen(Qt::red,2));
p.save();
p.rotate(sec_Angle);
p.drawLine(0,-70,0,0);
p.restore();
p.setPen(QPen(Qt::blue,4));
p.save();
p.rotate(min_Angle);
p.drawLine(0,-50,0,0);
p.restore();
p.setPen(QPen(Qt::green,6));
p.save();
p.rotate(hour_Angle);
p.drawLine(0,-30,0,0);
p.restore();
}
void Widget::updateClock()
{
count ++;
sec_Angle = 6.0*sec_A + 6.0*count;
min_Angle = 6.0*min_A + sec_A*6.0/60 + count*6.0/60;
hour_Angle = 30.0*hour_A + min_A*6.0/12 + sec_A*6.0/60/12 + count*6.0/60/12;
update();
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2f55151dfe7ec9bcf609e70bfffbf2fb.png)