简易闹钟
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTime>
#include <QTimer>
#include <QTimerEvent>
#include <QDateTime>
#include <QtTextToSpeech/QTextToSpeech>
#include <QFont>
#include <QFontDialog>
#include <QMouseEvent>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void timerEvent(QTimerEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
private slots:
void on_StartBtn_clicked();
void on_CloseBtn_clicked();
private:
Ui::Widget *ui;
int t_id;
QTextToSpeech *s;
QPoint StartPoint;
};
#endif
源代码
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
s = new QTextToSpeech(this);
setWindowFlag(Qt::FramelessWindowHint);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_StartBtn_clicked()
{
if(ui->StartBtn->text() == "启动")
{
t_id = this->startTimer(1000);
ui->StartBtn->setText("闹钟模式");
}
}
void Widget::on_CloseBtn_clicked()
{
if(ui->CloseBtn->text() == "关闭")
{
this->killTimer(t_id);
ui->StartBtn->setText("启动");
}
}
void Widget::timerEvent(QTimerEvent *event)
{
if(event->timerId() == t_id)
{
QDateTime sysDate = QDateTime::currentDateTime();
QString date = sysDate.toString("hh:mm:ss");
QFont f;
f.setPointSize(35);
ui->TimeLab->setAlignment(Qt::AlignHCenter);
ui->TimeLab->setFont(f);
ui->TimeLab->setText(date);
if(date == ui->TimeEdit->text())
{
s->say(ui->textEdit->toPlainText());
}
}
}
void Widget::mousePressEvent(QMouseEvent *event)
{
StartPoint = event->globalPos() - this->frameGeometry().topLeft();
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
this->move(event->globalPos()-StartPoint);
}
运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/495656d567dd04b8f675849f328aac93.png)