QT 使用定时器完成闹钟
#include "widget.h"
#include "ui_widget.h"void Widget::timestart()
{timer.start(1000);
}void Widget::timeend()
{timer.stop();
}Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);this->resize(800,550);this->setFixedSize(800,550);this->setWindowTitle("Alarm");lab1 = new QLabel(this);lab1->resize(360,120);lab1->move(80,80);lab1->setStyleSheet("background-color:rgb(225, 255, 248);font: 36pt;border-radius:10px");txtedt = new QTextEdit(this);txtedt->resize(650,250);txtedt->move(80,250);txtedt->setStyleSheet("background-color:rgb(225, 255, 248);font: 36pt;border-radius:10px");linedt1 = new QLineEdit(this);linedt1->resize(250,50);linedt1->move(480,80);linedt1->setPlaceholderText("预定时间");linedt1->setStyleSheet("background-color:rgb(225, 255, 248);font: 36pt;border-radius:10px");btn1 = new QPushButton("启动",this);btn1->resize(125,50);btn1->move(480,150);btn1->setStyleSheet("background-color:rgb(225, 255, 248);font: 36pt;border-radius:10px");QPushButton *btn2 = new QPushButton("停止",this);btn2->resize(125,50);btn2->move(610,150);btn2->setStyleSheet("background-color:rgb(225, 255, 248);font: 36pt;border-radius:10px");speecher = new QTextToSpeech;connect(btn1, &QPushButton::clicked, this, &Widget::timestart);connect(btn2, &QPushButton::clicked, this, &Widget::timeend);connect(&timer,&QTimer::timeout,this,&Widget::timer_slots);connect(&timer,&QTimer::timeout,this,&Widget::timealarm);
}Widget::~Widget()
{delete ui;
}
void Widget::timer_slots()
{QTime sysTime = QTime::currentTime();QString time = sysTime.toString("hh : mm : ss");lab1->setText(time);
}void Widget::timealarm()
{QTime sysTime = QTime::currentTime();//将Qtime转换为QstringQString time = sysTime.toString("hh:mm:ss");if(linedt1->text()==time){QString msg = txtedt->toPlainText();speecher->say(msg);}
}
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QLabel>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include <QTimer> //定时器类
#include <QTime>
#include <QTimerEvent>
#include <QDateTime>
#include <QtTextToSpeech> //文本转语音类
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECT
public slots:void timestart();void timeend();void timer_slots();void timealarm();
public:Widget(QWidget *parent = nullptr);~Widget();private:Ui::Widget *ui;int tid = 0;QTimer timer;QPushButton *btn1;QLabel *lab1;QTextToSpeech *speecher;QLineEdit *linedt1;QTextEdit *txtedt;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"void Widget::timeslot()
{s+=6;if(s==360){s=0;m+=12;}if(m==360){m=0;h+=30;}if(h==360){h=0;}this->update();
}Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);connect(&timer,&QTimer::timeout,this,&Widget::timeslot);timer.start(1000);}Widget::~Widget()
{delete ui;
}void Widget::paintEvent(QPaintEvent *event)
{QTime sysTime = QTime::currentTime();//将Qtime转换为QstringQString time = sysTime.toString("hh:mm:ss");//实例化一个画家QPainter painter(this);//2、实例化一个画笔QPen pen(QColor("skyblue"));pen.setWidth(10);//给画家安排上画笔painter.setPen(pen);//3、实例化一个字体QFont font("华文新魏",10);painter.setFont(font);//使用画家画文本文字//painter.drawText(this->rect(),Qt::AlignCenter,"好好学习,天天向上");painter.translate(QPoint(this->width()/2,this->height()/2));painter.drawEllipse(QPoint(0,0),200,200);for(int i=0;i<=11;i++){painter.drawLine(0,195,0,200);painter.rotate(30);}QPainter painter_s(this);QPen pen_s(QColor("green"));pen_s.setWidth(5);//给画家安排上画笔painter_s.translate(QPoint(this->width()/2,this->height()/2));painter_s.setPen(pen_s);painter_s.rotate(s);painter_s.drawLine(0,0,0,150);QPainter painter_m(this);QPen pen_m(QColor("yellow"));pen_m.setWidth(7);//给画家安排上画笔painter_m.translate(QPoint(this->width()/2,this->height()/2));painter_m.setPen(pen_m);painter_m.rotate(m);painter_m.drawLine(0,0,0,120);QPainter painter_h(this);QPen pen_h(QColor("red"));pen_h.setWidth(4);//给画家安排上画笔painter_h.translate(QPoint(this->width()/2,this->height()/2));painter_h.setPen(pen_h);painter_h.rotate(h);painter_h.drawLine(0,0,0,80);
}