Qt自制一個小鬧鐘的實現(xiàn)示例
更新時間:2023年09月05日 09:23:17 作者:ck釘釘釘
本文主要介紹了Qt自制一個小鬧鐘的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
功能
當按下啟動按鈕時,停止按鈕可用,啟動按鈕不可用,鬧鐘無法設(shè)置,無法輸入自定義內(nèi)容
當按下停止按鈕時,暫停播報,啟動按鈕可用,鬧鐘可以設(shè)置,可以輸入自定義內(nèi)容
.pro文件
QT += core gui texttospeech greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ widget.cpp HEADERS += \ widget.h FORMS += \ widget.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target RESOURCES += \ Icon.qrc
widget.h文件
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QTimer> //定時器類 #include <QTime> //時間類 #include <QTimerEvent> //定時器事件類的頭文件 #include <QDateTime> //日期時間類 #include <QDateTimeEdit> #include <QDebug> #include <QTextToSpeech> //朗讀 #include <QTextEdit> #include <QMetaObject> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); //重寫定時器事件處理函數(shù) void timerEvent(QTimerEvent *event)override; signals: void my_signal(); private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); private: Ui::Widget *ui; //定義一個定時器的id int timer_id; //基于事件處理函數(shù)的定時器 int timer_id1; QTextToSpeech *speech; int i = 0; int flag = 0; QString text; QDateTime sys_dt; }; #endif // WIDGET_H
main.cpp文件
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
widget.cpp文件
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); this->setWindowTitle("小鬧鐘"); timer_id = this->startTimer(5); ui->pushButton_2->setEnabled(false); ui->textEdit->setPlaceholderText("請輸入鬧鐘響時播報的內(nèi)容"); this->setWindowIcon(QIcon(":/new/prefix1/666.png")); } Widget::~Widget() { delete ui; } void Widget::timerEvent(QTimerEvent *event) { if(event->timerId()) //== timer_id) //用來判斷不同的定時器的id { //獲取當前系統(tǒng)的日期時間 sys_dt = QDateTime::currentDateTime(); //展示時間到ui界面的lable2中 ui->label->setText(sys_dt.toString("yyyy-MM-dd hh:mm:ss")); //居中顯示 標簽文本對齊方式 ui->label->setAlignment(Qt::AlignCenter); ui->label->setFont(QFont("微軟雅黑",20)); QString timeText = sys_dt.toString("yyyy-MM-dd hh:mm:ss"); QString timeText1 = ui->dateTimeEdit->text(); if(flag == 1) { if(timeText1 == timeText) { speech->say(text); // 朗讀文本 } } } } void Widget::on_pushButton_clicked() { flag = 1; speech = new QTextToSpeech; text = ui->textEdit->toPlainText(); ui->pushButton_2->setEnabled(true); ui->pushButton->setEnabled(false); ui->textEdit->setEnabled(false); ui->dateTimeEdit->setEnabled(false); } void Widget::on_pushButton_2_clicked() { flag = 0; ui->pushButton->setEnabled(true); ui->pushButton_2->setEnabled(false); ui->textEdit->setEnabled(true); ui->dateTimeEdit->setEnabled(true); speech->stop(); }
widget.ui文件
到此這篇關(guān)于Qt自制一個小鬧鐘的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Qt 鬧鐘內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++類與對象深入之靜態(tài)成員與友元及內(nèi)部類詳解
朋友們好,這篇播客我們繼續(xù)C++的初階學(xué)習(xí),現(xiàn)在對我們對C++的靜態(tài)成員,友元,內(nèi)部類知識點做出總結(jié),整理出來一篇博客供我們一起復(fù)習(xí)和學(xué)習(xí),如果文章中有理解不當?shù)牡胤?還希望朋友們在評論區(qū)指出,我們相互學(xué)習(xí),共同進步2022-06-06c++動態(tài)內(nèi)存空間示例(自定義空間類型大小和空間長度)
這篇文章主要介紹了c++動態(tài)內(nèi)存空間示例,自定義空間類型大小和空間長度,需要的朋友可以參考下2014-04-04