簡單實現鬧鐘播報,設置時間,當系統時間與設置時間相同時播報語音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("啟動");}
}