头文件
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include<QPainter>//画家类
#include<QTime>//时间类
#include<QTimer>//定时器类QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void timeout_slot();void paintEvent(QPaintEvent *event) override;private:Ui::Widget *ui;QTimer q;int h=0,m=0,s=0;
};
#endif // WIDGET_H
源文件
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);this->resize(500,500);q.start(1000);connect(&q,&QTimer::timeout,this,&Widget::timeout_slot);
}Widget::~Widget()
{delete ui;
}void Widget::paintEvent(QPaintEvent *event)
{QPainter maliang(this);QPen pen;pen.setStyle(Qt::SolidLine);pen.setColor(QColor("red"));maliang.setBrush(QColor("skyblue"));pen.setWidth(10);maliang.setPen(pen);//maliang.drawEllipse(250,250,100,100);maliang.translate(QPoint(250,250));maliang.drawEllipse(QPoint(0,0),150,150);for(int i=0;i<12;i++){maliang.rotate(30);maliang.drawLine(QPoint(0,130),QPoint(0,150));}for(int i=0;i<60;i++){pen.setWidth(4);maliang.setPen(pen);maliang.rotate(6);maliang.drawLine(QPoint(0,140),QPoint(0,150));}for(int i=0;i<12;i++){static int num=1;QString str=QString::number(num);maliang.rotate(30);maliang.drawText(QPoint(0,-110),str);if(num==0){num=12;}if(num>=12){num=num-12;}num++;}QPainter maliang1(this);maliang1.translate(QPoint(250,250));QPen pen1;pen1.setColor(QColor("green"));pen1.setWidth(12);maliang1.setPen(pen1);maliang1.rotate(h*30);maliang1.drawLine(QPoint(0,0),QPoint(0,-80));QPainter maliang2(this);maliang2.translate(QPoint(250,250));QPen pen2;pen2.setColor(QColor("yellow"));pen2.setWidth(8);maliang2.setPen(pen2);maliang2.rotate(m*6);maliang2.drawLine(QPoint(0,0),QPoint(0,-90));QPainter maliang3(this);maliang3.translate(QPoint(250,250));QPen pen3;pen3.setColor(QColor("black"));pen3.setWidth(6);maliang3.setPen(pen3);maliang3.rotate(s*6);maliang3.drawLine(QPoint(0,0),QPoint(0,-100));this->update();}
void Widget::timeout_slot()
{QTime systime=QTime::currentTime();h=systime.hour();if(h>12){h=h-12;}else if(h==24){h=12;}m=systime.minute();s=systime.second();}
头文件
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include<QTime>
#include<QTimer>
#include<QMessageBox>
#include <QTextToSpeech>
#include<qstring.h>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void timeout_slot();void timerEvent(QTimerEvent *event)override;private slots://void on_label_linkActivated(const QString &link);void on_pushButton_clicked();private:Ui::Widget *ui;QTimer *objtimer;int eventtimeid;int clocktimeid;QTextToSpeech *speecher;int btn;
};
#endif // WIDGET_H
源文件
#include "widget.h"
#include "ui_widget.h"
#include<qdebug.h>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);QTimer *objtimer;objtimer=new QTimer(this);connect(objtimer,&QTimer::timeout,this,&Widget::timeout_slot);objtimer->start(1000);//eventimeid=this->startTimer(1000);timeout_slot();speecher = new QTextToSpeech(this);}Widget::~Widget()
{delete ui;
}
void Widget::timerEvent(QTimerEvent *event)
{if(event->timerId()==clocktimeid){if(ui->lineEdit->text()==ui->label->text()){//qDebug()<<"??????";QString msg="现在是北京时间"+ui->label->text()+"到时间了,打屁屁咯";speecher->say(msg);this->killTimer(clocktimeid);}}}
void Widget::timeout_slot()
{QTime time = QTime::currentTime();QString t=time.toString("hh::mm::ss");ui->label->setText(t);}void Widget::on_pushButton_clicked()
{btn= QMessageBox::question( this,"问题","确定时间吗",QMessageBox::Yes|QMessageBox::No);if(btn==QMessageBox::No){ui->lineEdit->clear();}clocktimeid=this->startTimer(1000);
}