基于QT實現(xiàn)簡單的鬧鐘
鬧鐘效果
時鐘顯示 通過 QTimer 每秒更新一次 QLCDNumber 顯示的當前時間,格式為 hh:mm:ss,實現(xiàn)實時時鐘顯示。
1.鬧鐘設(shè)置
使用 QDateTimeEdit 讓用戶設(shè)置鬧鐘時間,可通過日歷選擇日期,設(shè)置范圍為當前時間到未來 10 天。
2.提醒功能
語音播報:當?shù)竭_設(shè)定的鬧鐘時間,從 QPlainTextEdit 獲取文本,利用 QTextToSpeech 進行語音播報。
音效播放:同時,使用 QMediaPlayer 播放指定的提醒音效文件 123.mp3,音量設(shè)為 80。
3.控制操作
啟動鬧鐘:點擊“啟動”按鈕,開啟定時器進行時間檢查,此時“啟動”按鈕禁用,“停止”按鈕啟用。
停止鬧鐘:點擊“停止”按鈕,停止定時器和音效播放,“啟動”按鈕重新啟用,“停止”按鈕禁用。
完整代碼
#include "widget.h" #include "ui_widget.h" #include<QTime> #include<QDateTime> #include<QTextToSpeech> #include<QDebug> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); //創(chuàng)建時鐘 timerTime=new QTimer(this); timerClock=new QTimer(this); //綁定時鐘函數(shù) connect(timerTime,&QTimer::timeout,this,&Widget::timeSlot); connect(timerClock,&QTimer::timeout,this,&Widget::clockSlot); //時鐘直接啟動 timerTime->start(1000); ui->timeLCD->setDigitCount(8); ui->dateTimeEdit->setCalendarPopup(true); ui->dateTimeEdit->setMinimumDateTime(QDateTime::currentDateTime()); ui->dateTimeEdit->setMaximumDate(QDate::currentDate().addDays(10)); ui->dateTimeEdit->setDisplayFormat("yyyy-MM-dd hh:mm:ss"); speech=new QTextToSpeech(this); startSound=new QMediaPlayer(this); startSound->setMedia(QUrl("qrc:/new/prefix1/123.mp3")); startSound->setVolume(80); //音量 } Widget::~Widget() { delete ui; } void Widget::on_startBtn_clicked() { //計時器啟動 this->timerClock->start(1000); ui->startBtn->setEnabled(false); ui->endBtn->setEnabled(true); } void Widget::on_endBtn_clicked() { // 取消 this->timerClock->stop(); startSound->stop(); ui->startBtn->setEnabled(true); ui->endBtn->setEnabled(false); } //時鐘增加函數(shù) void Widget::timeSlot() { QTime time; ui->timeLCD->display(time.currentTime().toString("hh:mm:ss")); } //鬧鐘函數(shù) void Widget::clockSlot() { QDateTime dt=ui->dateTimeEdit->dateTime(); // QDateTime now; if(dt.secsTo(QDateTime::currentDateTime())==0){ //播報 QString text=ui->plainTextEdit->toPlainText(); speech->say(text); ui->startBtn->setEnabled(true); timerClock->stop(); startSound->play(); } }
學習筆記
QObject::event
函數(shù)名:bool QObject::event(QEvent *e)
函數(shù)功能:這是Qt事件處理的入口函數(shù)。當一個事件發(fā)生時,首先會調(diào)用該函數(shù)。它會根據(jù)傳入的事件對象e,識別事件類型,然后進行初步處理或分發(fā)到更具體的事件處理函數(shù)。比如,它可以判斷事件是鼠標事件、鍵盤事件還是其他類型事件,進而決定后續(xù)處理流程。
函數(shù)參數(shù):QEvent *e,這是一個指向QEvent對象的指針。QEvent是所有事件類的基類,通過它可以獲取事件的詳細信息,如事件類型、發(fā)生時間等。
函數(shù)返回值:bool類型。如果事件被成功處理,返回true;若未處理或需要進一步處理,則返回false。返回值會影響事件的后續(xù)傳遞,如果返回false,事件可能會繼續(xù)向上傳遞給父對象處理。
主要函數(shù)舉例:
class MyObject : public QObject { Q_OBJECT public: bool event(QEvent *e) override { if (e->type() == QEvent::MouseButtonPress) { qDebug() << "Mouse button pressed event caught in event()"; return true; } return QObject::event(e); } };
在這個例子中,MyObject類繼承自QObject并重寫了event函數(shù)。當檢測到鼠標按下事件時,輸出相應(yīng)信息并返回true,表示事件已處理;否則,調(diào)用父類的event函數(shù)繼續(xù)處理事件。
QWidget的鼠標事件處理函數(shù)
mousePressEvent
函數(shù)名:void QWidget::mousePressEvent(QMouseEvent *event)
函數(shù)功能:當鼠標按鍵在部件上按下時被調(diào)用。常用于記錄鼠標按下的位置,為后續(xù)的繪圖、拖曳等操作做準備。
函數(shù)參數(shù):QMouseEvent *event,通過它可以獲取鼠標事件的詳細信息,包括按下的是哪個鼠標按鍵(如Qt::LeftButton、Qt::RightButton等),鼠標在部件上的位置(相對于部件的坐標pos())以及在屏幕上的全局坐標(globalPos())等。
函數(shù)返回值:void,沒有返回值,專注于處理鼠標按下事件的相關(guān)邏輯。
主要函數(shù)舉例:
class MyWidget : public QWidget { Q_OBJECT public: void mousePressEvent(QMouseEvent *event) override { if (event->button() == Qt::LeftButton) { qDebug() << "Left mouse button pressed at local position:" << event->pos(); } else if (event->button() == Qt::RightButton) { qDebug() << "Right mouse button pressed at global position:" << event->globalPos(); } } };
在這個示例中,MyWidget類重寫了mousePressEvent函數(shù),根據(jù)按下的鼠標按鍵不同,輸出相應(yīng)的位置信息。
mouseReleaseEvent
函數(shù)名:void QWidget::mouseReleaseEvent(QMouseEvent *event)
函數(shù)功能:在鼠標按鍵在部件上釋放時被調(diào)用。常與mousePressEvent配合使用,完成一些與鼠標操作相關(guān)的功能,比如在繪圖應(yīng)用中,鼠標按下時開始繪制,釋放時結(jié)束繪制。
函數(shù)參數(shù):QMouseEvent *event,包含鼠標釋放事件的詳細信息,與mousePressEvent中的參數(shù)類似。
函數(shù)返回值:void,沒有返回值,主要用于處理鼠標釋放后的相關(guān)操作。
主要函數(shù)舉例:
class MyWidget : public QWidget { Q_OBJECT QPoint pressPos; public: void mousePressEvent(QMouseEvent *event) override { if (event->button() == Qt::LeftButton) { pressPos = event->pos(); } } void mouseReleaseEvent(QMouseEvent *event) override { if (event->button() == Qt::LeftButton) { QPoint releasePos = event->pos(); int distance = (releasePos - pressPos).manhattanLength(); qDebug() << "Mouse dragged for a distance of:" << distance; } } };
此例中,MyWidget類在mousePressEvent中記錄鼠標按下的位置,在mouseReleaseEvent中計算鼠標拖動的距離并輸出。
mouseDoubleClickEvent
函數(shù)名:void QWidget::mouseDoubleClickEvent(QMouseEvent *event)
函數(shù)功能:當鼠標在部件上雙擊時被調(diào)用。通常用于實現(xiàn)一些特殊的交互功能,如雙擊打開文件、放大視圖等。
函數(shù)參數(shù):QMouseEvent *event,可獲取雙擊事件的相關(guān)信息,如雙擊的位置、按鍵等。
函數(shù)返回值:void,沒有返回值,用于執(zhí)行雙擊事件對應(yīng)的操作。
主要函數(shù)舉例:
class MyWidget : public QWidget { Q_OBJECT public: void mouseDoubleClickEvent(QMouseEvent *event) override { if (event->button() == Qt::LeftButton) { qDebug() << "Left mouse button double - clicked at:" << event->pos(); } } };
在這個示例中,當鼠標左鍵在MyWidget上雙擊時,輸出雙擊的位置信息。
mouseMoveEvent
函數(shù)名:void QWidget::mouseMoveEvent(QMouseEvent *event)
函數(shù)功能:當鼠標在部件上移動時被調(diào)用。常用于實現(xiàn)實時響應(yīng)鼠標移動的功能,如在繪圖軟件中實時繪制線條,或在游戲中根據(jù)鼠標移動控制角色方向。
函數(shù)參數(shù):QMouseEvent *event,通過它可以獲取鼠標移動的詳細信息,包括當前鼠標位置(pos())、移動過程中按下的按鍵(buttons())等。
函數(shù)返回值:void,沒有返回值,專注于處理鼠標移動相關(guān)邏輯。
主要函數(shù)舉例:
class MyWidget : public QWidget { Q_OBJECT public: void mouseMoveEvent(QMouseEvent *event) override { if (event->buttons() & Qt::LeftButton) { qDebug() << "Mouse is being dragged with left button at:" << event->pos(); } } };
在這個例子中,當鼠標左鍵被按下并移動時,輸出鼠標的當前位置信息。
QWidget的鍵盤事件處理函數(shù)
keyPressEvent
函數(shù)名:void QWidget::keyPressEvent(QKeyEvent *event)
函數(shù)功能:在鍵盤按鍵被按下時被調(diào)用。常用于實現(xiàn)各種鍵盤控制功能,如在游戲中通過鍵盤控制角色移動,在文本編輯器中處理按鍵輸入等。
函數(shù)參數(shù):QKeyEvent *event,通過它可以獲取按下按鍵的詳細信息,如按鍵的鍵值(key(),用于識別具體按鍵)、本次事件中使用的鍵的個數(shù)(count())以及鍵上的文本內(nèi)容(text())等。
函數(shù)返回值:void,沒有返回值,主要用于執(zhí)行鍵盤按下事件對應(yīng)的操作。
主要函數(shù)舉例:
class MyWidget : public QWidget { Q_OBJECT public: void keyPressEvent(QKeyEvent *event) override { if (event->key() == Qt::Key_A) { qDebug() << "The 'A' key was pressed"; } else if (event->key() == Qt::Key_Up) { qDebug() << "The up arrow key was pressed"; } } };
在此例中,MyWidget類重寫了keyPressEvent函數(shù),根據(jù)按下的不同按鍵輸出相應(yīng)信息。
keyReleaseEvent
函數(shù)名:void QWidget::keyReleaseEvent(QKeyEvent *event)
函數(shù)功能:當鍵盤按鍵被釋放時被調(diào)用??捎糜谔幚砼c按鍵釋放相關(guān)的邏輯,如在一些需要長按按鍵觸發(fā)特殊功能的場景中,按鍵釋放時結(jié)束該功能。
函數(shù)參數(shù):QKeyEvent *event,包含按鍵釋放事件的詳細信息,與keyPressEvent中的參數(shù)類似。
函數(shù)返回值:void,沒有返回值,專注于處理按鍵釋放后的操作。
主要函數(shù)舉例:
class MyWidget : public QWidget { Q_OBJECT bool isCtrlPressed = false; public: void keyPressEvent(QKeyEvent *event) override { if (event->key() == Qt::Key_Control) { isCtrlPressed = true; } } void keyReleaseEvent(QKeyEvent *event) override { if (event->key() == Qt::Key_Control) { isCtrlPressed = false; qDebug() << "Ctrl key released"; } } };
在這個示例中,MyWidget類通過keyPressEvent和keyReleaseEvent函數(shù)記錄Ctrl鍵的按下和釋放狀態(tài),并在Ctrl鍵釋放時輸出相應(yīng)信息。
QWidget的定時器事件處理函數(shù)
函數(shù)名:void QWidget::timerEvent(QTimerEvent *event)
函數(shù)功能:當定時器超時時被調(diào)用。在基于事件處理函數(shù)版本的定時器實現(xiàn)中,通過重寫該函數(shù)來定義定時器超時后執(zhí)行的操作,比如定時更新界面顯示的時間、定時檢查網(wǎng)絡(luò)連接狀態(tài)等。
函數(shù)參數(shù):QTimerEvent *event,通過它可以獲取定時器的相關(guān)信息,如定時器的ID號(timerId()),用于區(qū)分不同的定時器(當存在多個定時器時)。
函數(shù)返回值:void,沒有返回值,主要用于執(zhí)行定時器超時后的操作。
主要函數(shù)舉例:
class MyWidget : public QWidget { Q_OBJECT int timerId; public: MyWidget(QWidget *parent = nullptr) : QWidget(parent) { timerId = startTimer(1000); // 啟動一個每秒觸發(fā)一次的定時器 } void timerEvent(QTimerEvent *event) override { if (event->timerId() == timerId) { qDebug() << "Timer timeout. Current time:" << QTime::currentTime(); } } };
在這個例子中,MyWidget類啟動了一個定時器,并在timerEvent函數(shù)中處理定時器超時事件,輸出當前時間。
QWidget的繪制事件處理函數(shù)
函數(shù)名:void QWidget::paintEvent(QPaintEvent *event)
函數(shù)功能:用于處理繪制事件,當窗口需要重新繪制時(如窗口大小改變、最小化后恢復(fù)、主動調(diào)用repaint或update函數(shù)等情況),該函數(shù)會被自動調(diào)用。通常在這個函數(shù)中使用QPainter類進行各種圖形繪制操作,如繪制文本、矩形、橢圓等。
函數(shù)參數(shù):QPaintEvent *event,包含了繪制事件的相關(guān)信息,雖然在實際繪制操作中可能較少直接使用,但它是繪制事件的標識。
函數(shù)返回值:void,主要負責執(zhí)行繪制操作,沒有返回值。
主要函數(shù)舉例:
class MyWidget : public QWidget { Q_OBJECT public: void paintEvent(QPaintEvent *event) override { QPainter painter(this); painter.setPen(Qt::blue); painter.setFont(QFont("Arial", 20)); painter.drawText(rect(), Qt::AlignCenter, "Hello, Qt!"); painter.drawRect(10, 10, width() - 20, height() - 20); } };
在這個示例中,MyWidget類重寫了paintEvent函數(shù),使用QPainter在窗口中繪制了文本和矩形。
到此這篇關(guān)于基于QT實現(xiàn)簡單的鬧鐘的文章就介紹到這了,更多相關(guān)QT鬧鐘內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
linux內(nèi)核select/poll,epoll實現(xiàn)與區(qū)別
這篇文章主要介紹了linux內(nèi)核select/poll,epoll實現(xiàn)與區(qū)別,需要的朋友可以參考下2016-11-11C語言SetConsoleCursorPosition函數(shù)使用方法
這篇文章介紹了C語言SetConsoleCursorPosition函數(shù)的使用方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-12-12