1.绘制时针
xx.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimer>
#include<QPainter>
#include <QTime>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void paintEvent(QPaintEvent *event) override;
private:
Ui::Widget *ui;
QTimer *timer;
private slots:
void updateClock();
};
#endif // WIDGET_H
xxx.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &Widget::updateClock);
timer->start(1000); // 每秒更新一次
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *event)
{
QTime time = QTime::currentTime();//获取当前时间
int hour = time.hour();
int minute = time.minute();
int second = time.second();
QPainter painter(this); // 实例化对象
painter.setRenderHint(QPainter::Antialiasing); //设置渲染,消除锯齿装
// 设置时钟中心
int side = qMin(width(), height());
painter.translate(width() / 2, height() / 2);//将坐标移到中心
painter.scale(side / 200, side / 200); //根据窗口,设置绘制的比例
// 绘制时针
painter.save();
painter.rotate(30.0 * ((hour + minute / 60.0)));//绘制时针的旋转
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::black);
painter.drawRect(-5, -50, 10, 50);
painter.restore();
// 绘制分针
painter.save();
painter.rotate(6.0 * (minute + second / 60.0));
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::darkGray);
painter.drawRect(-3, -70, 6, 70);
painter.restore();
// 绘制秒针
painter.save();
painter.rotate(6.0 * second);
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::red);
painter.drawRect(-2, -90, 4, 90);
painter.restore();
// 绘制时钟刻度
painter.setPen(Qt::black);
for (int i = 0; i < 12; ++i) {
painter.drawLine(88, 0, 96, 0);
painter.rotate(30.0);
}
// 绘制小刻度
painter.setPen(Qt::gray);
for (int i = 0; i < 60; ++i) {
if (i % 5 != 0) { // 跳过大刻度的位置
painter.drawLine(92, 0, 96, 0);
}
painter.rotate(6.0);
}
}
void Widget::updateClock()
{
update();
}
2. 闹钟播报
xxx.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QMessageBox>
#include<QTime>
#include<QTimer>
#include <QTextToSpeech>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void timeout_slot1();
void timeout_slot2();
void on_startButton_clicked();
void on_closeButton_clicked();
private:
Ui::Widget *ui;
QTimer *timer1;
QTimer *timer2;
QTextToSpeech *speecher;
};
#endif // WIDGET_H
xxx.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
speecher = new QTextToSpeech(this);
timer1 = new QTimer(this);
timer2 = new QTimer(this);
timer1->start(1000);
connect(timer1, &QTimer::timeout, this, &Widget::timeout_slot1);
connect(timer2, &QTimer::timeout, this, &Widget::timeout_slot2);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timeout_slot1()
{
QTime sysTime = QTime::currentTime();
QString t = sysTime.toString("hh:mm:ss");
ui->curtimelab->setText(t);
}
void Widget::timeout_slot2()
{
if(ui->curtimelab->text()==ui->settimeEdit->text())
{
qDebug()<<"时间到";
QString msg ="现在是北京时间"+ui->settimeEdit->text()+ui->textEdit->toPlainText();
speecher->say(msg);
timer2->stop();
ui->startButton->setDisabled(false);
ui->textEdit->setDisabled(false);
}
}
void Widget::on_startButton_clicked()
{
timer2->start(1000);
ui->startButton->setDisabled(true);
ui->textEdit->setDisabled(true);
}
void Widget::on_closeButton_clicked()
{
timer2->stop();
ui->startButton->setDisabled(false);
ui->textEdit->setDisabled(false);
}