QT(制作一个闹钟)

//.pro配置文件
#-------------------------------------------------
#
# Project created by QtCreator 2023-06-15T11:31:23
#
#-------------------------------------------------

QT       += core gui texttospeech

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = alarm_clock
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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

CONFIG += c++11

SOURCES += \
        main.cpp \
        my_qt.cpp

HEADERS += \
        my_qt.h

FORMS += \
        my_qt.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

头文件

#ifndef MY_QT_H
#define MY_QT_H

#include <QWidget>
#include <QTimerEvent> //事件类
#include <QDateTime> //日期时间类
#include <QDebug>
#include <QTextToSpeech>
#include <QPushButton>
namespace Ui {
class my_qt;
}

class my_qt : public QWidget
{
    Q_OBJECT

public:
    explicit my_qt(QWidget *parent = nullptr);
    ~my_qt();
    void timerEvent(QTimerEvent *e)override;
private slots:
    void on_btn_start_clicked();

    void on_btn_close_clicked();

private:
    Ui::my_qt *ui;

    int tid,tid2;
    QTextToSpeech *tell;

};

#endif // MY_QT_H

main函数

#include "my_qt.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    my_qt w;
    w.show();

    return a.exec();
}

运行函数

#include "my_qt.h"
#include "ui_my_qt.h"

my_qt::my_qt(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::my_qt)
{
    ui->setupUi(this);
    //启动一个定时器
    tid = this->startTimer(1000);
    tell = new QTextToSpeech(this);
    tell->setLocale(QLocale::Chinese);
}

my_qt::~my_qt()
{
    delete ui;
}

void my_qt::on_btn_start_clicked()
{
    //将启动按钮禁用
    ui->btn_start->setEnabled(0);
    //将文本框禁用
    ui->textEdit->setEnabled(0);
    ui->timer_label->setEnabled(0);





}

void my_qt::on_btn_close_clicked()
{
    //将启动按钮启用
    ui->btn_start->setEnabled(1);
    //将文本框启用
    ui->textEdit->setEnabled(1);
    ui->timer_label->setEnabled(1);
    //关闭一个定时器
    this->killTimer(tid);

}
void my_qt::timerEvent(QTimerEvent *e){
    if(e->timerId() == tid){
        ui->time_label->setAlignment(Qt::AlignCenter);//将显示文本居中显示
        QDateTime t = QDateTime::currentDateTime();
        ui->time_label->setText(t.toString("yyyy/MM/dd hh:mm:ss"));
        if(ui->timer_label->text()== ui->time_label->text()){
            tell->say(ui->textEdit->toPlainText());
        }

    }

}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值