#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QTextEdit>
#include <QTimerEvent>
#include <QTimer>
#include <QDateTime>
#include <QDebug>
#include <cstring>
#include <QMouseEvent>
#include <QtTextToSpeech>
#include <QChar>
#include <QString>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void lab_datatime();
void startbtn_slots();
void stop_slots();
void timerEvent(QTimerEvent* event)override;
private:
QLabel* lab;
QLineEdit* ledit;
QPushButton* startbtn;
QPushButton* stopbtn;
QTextEdit* tedit;
QTimer timer;
QTextToSpeech* speecher;
int tid;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//this->resize(600,400);
this->setWindowTitle("闹钟");
this->setFixedSize(820,600);
lab = new QLabel(this);
lab->resize(350,150);
lab->move(70,70);
lab->setStyleSheet("background-color:green");
ledit = new QLineEdit(this);
ledit->resize(320,70);
ledit->move(440,70);
ledit->setFont(QFont("宋体",20));
startbtn = new QPushButton("启动",this);
startbtn->resize(145,55);
startbtn->move(440,160);
stopbtn = new QPushButton("停止",this);
stopbtn->resize(145,55);
stopbtn->move(610,160);
tedit=new QTextEdit("三更灯火五更鸡,"
"正式男儿读书时;"
"黑发布置勤学早,"
"白首方悔读书迟。"
,this);
tedit->setFont(QFont("宋体",20));
tedit->resize(700,250);
tedit->move(70,300);
timer.start(1000);
connect(&timer,&QTimer::timeout,this,&Widget::lab_datatime);
connect(startbtn,&QPushButton::clicked,this,&Widget::startbtn_slots);
connect(stopbtn,&QPushButton::clicked,this,&Widget::stop_slots);
speecher = new QTextToSpeech;
}
Widget::~Widget()
{
}
void Widget::lab_datatime()
{
QDateTime sysdati=QDateTime::currentDateTime();
QString datatime=sysdati.toString(" yyyy - MM - dd \n"
" hh : mm : ss");
this->lab->setText(datatime);
this->lab->setFont(QFont("宋体",20));
}
void Widget::startbtn_slots()
{
if(startbtn->text()=="启动")
{
tid=this->startTimer(1000);
}
}
void Widget::stop_slots()
{
this->killTimer(tid);
}
void Widget::timerEvent(QTimerEvent* event)
{
QDateTime sysdati=QDateTime::currentDateTime();
QString datatime=sysdati.toString(" hh : mm : ss");
QString setdati=ledit->text();
if(datatime==setdati)
{
QString s2="现在是北京时间";
QString s3="点整";
speecher->say(QString("%1,%2,%3").arg(s2).arg(sysdati.toString("hh")).arg(s3));
}
}
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
id=startTimer(1000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *event)
{
//实例化一个画家
QPainter p(this);
//实例化画笔
QPen pen(QColor(QRgb(qRgb(0,0,0))));
// QPen pen(QColor("red"));//画笔颜色
// pen.setWidth(5);//画笔宽度
//将画笔给画家
p.setPen(pen);
//移动画家绘制中心
p.translate(QPoint(this->width()/2,this->height()/2));
//绘制一个圆
p.drawEllipse(QPoint(0,0),200,200);
//绘制刻度与整点刻度对应的数字
for(int i=0;i<60;i++){
if(i%5==0)//360度,6度1格,一共60格,每隔30度对应一个数字,一共12个
{
pen.setWidth(3);
p.setPen(pen);
if(i/5==0){
p.drawText(0,-175,"12");//0对应是数字12
}
else{
p.drawText(0,-175,QString("%1").arg(i/5));//每隔5度对应数字为刻度除以5
}
}
else //不是30度的整数倍的格子不标记数字
{
pen.setWidth(1);
p.setPen(pen);
}
//每加加一次需要旋转6度画一个刻度
p.drawLine(0,-195,0,-200);
p.rotate(6);
}
//绘制时针
//秒针
p.rotate(QTime().currentTime().second()*6);//秒针绘画需要一次旋转6度
p.drawLine(0,0,0,-150);
//时针 分针
p.rotate(360-QTime().currentTime().second()*6);
for(int i=0;i<60;i++){
p.rotate(6);
if(i*6==((QTime().currentTime().hour())-12)*30){
p.drawLine(0,0,0,-50);
}
else if(i*6==(QTime().currentTime().minute()*6)){
p.drawLine(0,0,0,-100);
}
}
}
void Widget::timerEvent(QTimerEvent *event){
this->update();
}