Qt自制一個(gè)小鬧鐘的實(shí)現(xiàn)示例
功能
當(dāng)按下啟動(dòng)按鈕時(shí),停止按鈕可用,啟動(dòng)按鈕不可用,鬧鐘無(wú)法設(shè)置,無(wú)法輸入自定義內(nèi)容
當(dāng)按下停止按鈕時(shí),暫停播報(bào),啟動(dòng)按鈕可用,鬧鐘可以設(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.qrcwidget.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimer> //定時(shí)器類
#include <QTime> //時(shí)間類
#include <QTimerEvent> //定時(shí)器事件類的頭文件
#include <QDateTime> //日期時(shí)間類
#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í)器事件處理函數(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;
//定義一個(gè)定時(shí)器的id
int timer_id; //基于事件處理函數(shù)的定時(shí)器
int timer_id1;
QTextToSpeech *speech;
int i = 0;
int flag = 0;
QString text;
QDateTime sys_dt;
};
#endif // WIDGET_Hmain.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("請(qǐng)輸入鬧鐘響時(shí)播報(bào)的內(nèi)容");
this->setWindowIcon(QIcon(":/new/prefix1/666.png"));
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *event)
{
if(event->timerId()) //== timer_id) //用來(lái)判斷不同的定時(shí)器的id
{
//獲取當(dāng)前系統(tǒng)的日期時(shí)間
sys_dt = QDateTime::currentDateTime();
//展示時(shí)間到ui界面的lable2中
ui->label->setText(sys_dt.toString("yyyy-MM-dd hh:mm:ss"));
//居中顯示 標(biāo)簽文本對(duì)齊方式
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自制一個(gè)小鬧鐘的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Qt 鬧鐘內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Qt圖形圖像開(kāi)發(fā)之曲線圖表庫(kù)QChart編譯安裝詳細(xì)方法與使用實(shí)例
這篇文章主要介紹了Qt圖形圖像開(kāi)發(fā)之曲線圖表庫(kù)QChart編譯安裝詳細(xì)方法與使用實(shí)例,需要的朋友可以參考下2020-03-03
C++簡(jiǎn)明講解缺省參數(shù)與函數(shù)重載的用法
所謂缺省參數(shù),顧名思義,就是在聲明函數(shù)的某個(gè)參數(shù)的時(shí)候?yàn)橹付ㄒ粋€(gè)默認(rèn)值,在調(diào)用該函數(shù)的時(shí)候如果采用該默認(rèn)值,你就無(wú)須指定該參數(shù)。C++ 允許多個(gè)函數(shù)擁有相同的名字,只要它們的參數(shù)列表不同就可以,這就是函數(shù)的重載,借助重載,一個(gè)函數(shù)名可以有多種用途2022-06-06
Qt物聯(lián)網(wǎng)管理平臺(tái)之實(shí)現(xiàn)告警短信轉(zhuǎn)發(fā)
系統(tǒng)在運(yùn)行過(guò)程中,會(huì)實(shí)時(shí)采集設(shè)備的數(shù)據(jù),當(dāng)采集到的數(shù)據(jù)發(fā)生報(bào)警后,可以將報(bào)警信息以短信的形式發(fā)送給指定的管理員。本文將利用Qt實(shí)現(xiàn)告警短信轉(zhuǎn)發(fā),感興趣的可以嘗試一下2022-07-07
QT使用canon sdk拍照并保存到本機(jī)的方法示例
這篇文章主要介紹了QT使用canon sdk拍照并保存到本機(jī)的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
C++ 二叉搜索樹(shù)(BST)的實(shí)現(xiàn)方法
這篇文章主要介紹了C++ 二叉搜索樹(shù)(BST)的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-04-04
vscode調(diào)試gstreamer源碼的詳細(xì)流程
在本文中主要介紹了如何使用vscode調(diào)試C++和python程序,并進(jìn)一步分析了如何調(diào)試gstreamer源碼,講述了如何調(diào)試gstreamer源碼的具體流程,感興趣的朋友跟隨小編一起看看吧2023-01-01
基于atoi()與itoa()函數(shù)的內(nèi)部實(shí)現(xiàn)方法詳解
本篇文章是對(duì)atoi()與itoa()函數(shù)的內(nèi)部實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
vc++實(shí)現(xiàn)的tcp socket客戶端和服務(wù)端示例
這篇文章主要介紹了vc++實(shí)現(xiàn)的tcp socket客戶端和服務(wù)端示例,需要的朋友可以參考下2014-03-03
C語(yǔ)言實(shí)現(xiàn)通訊錄的詳細(xì)代碼
本文詳細(xì)講解了C語(yǔ)言實(shí)現(xiàn)通訊錄的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12

