简单实现闹钟播报,设置时间,当系统时间与设置时间相同时播报语音5次,然后停止。如果设置时间小于当前系统时间,则弹出消息提示框,并清空输入框。
#include "my_clock.h"
#include "ui_my_clock.h"
#include "aerowidget.h"my_clock::my_clock(QWidget *parent): QWidget(parent), ui(new Ui::my_clock)
{ui->setupUi(this);this->setAttribute(Qt::WA_TranslucentBackground);ui->time_screen->setAlignment(Qt::AlignCenter);setWindowFlags(Qt::FramelessWindowHint);
// setWindowFlags(Qt::Tool | Qt::FramelessWindowHint |Qt::X11BypassWindowManagerHint); // 设置窗口标志为Qt::Tool、Qt::FramelessWindowHint和Qt::WindowMinimizeButtonHint// 设置鼠标追踪setMouseTracking(true);timer_id = startTimer(100);
}my_clock::~my_clock()
{delete ui;
}// 重写鼠标按下事件
void my_clock::mousePressEvent(QMouseEvent *event)
{if (event->button() == Qt::LeftButton) {// 保存鼠标按下时的位置和窗口位置m_dragPos = event->globalPos() - frameGeometry().topLeft();event->accept();}
}// 重写鼠标移动事件
void my_clock::mouseMoveEvent(QMouseEvent *event)
{if (event->buttons() & Qt::LeftButton) {// 移动窗口到鼠标位置move(event->globalPos() - m_dragPos);event->accept();}
}void my_clock::timerEvent(QTimerEvent *e)
{QDateTime dateTime = QDateTime::currentDateTime();if(e->timerId() == timer_id){ui->time_screen->setText(dateTime.toString("hh:mm:ss"));// ui->time_screen->setText(dateTime.toString("yyyy/MM/dd - hh:mm:ss"));}else if (e->timerId() == timer_id_btn){}
}void my_clock::on_pushButton_3_clicked()
{close();
}void my_clock::on_pushButton_4_clicked()
{
// showMinimized();Qt::WindowFlags flags = windowFlags();if (flags & Qt::WindowStaysOnTopHint)flags &= ~Qt::WindowStaysOnTopHint;elseflags |= Qt::WindowStaysOnTopHint;setWindowFlags(flags);show(); // Ensure the window is shown after changing flags
}void my_clock::on_pushButton_5_clicked()
{if(ui->pushButton_5->text() == "启动"){timer_id_btn = startTimer(1000);ui->pushButton_5->setText("关闭");}else{killTimer(timer_id_btn);ui->pushButton_5->setText("启动");}
}