Qt 定制专属闹钟

.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMouseEvent>
#include <QDebug>
#include <QTimerEvent>
#include<QTime>
#include <QTextToSpeech>
#include<QVoice>

#include <QTextEdit>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    void timerEvent(QTimerEvent *e) override;

private slots:
    void on_openBtn_clicked();

    void on_closeBtn_clicked();

private:
    Ui::Widget *ui;

    int tim;
   int respeech_time;
    //将组建设置为私有成员
    QTextEdit *speakEdit;
    //定义一个播报员
    QTextToSpeech *speech;
};
#endif // WIDGET_H

 .cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //更改窗口标题
    this->setWindowTitle("查理苏的专属闹钟");
    ui->logo->setPixmap(QPixmap(":/qt/a1.webp"));
    ui->logo->setScaledContents(true);

    //用户名密码图

    ui->larmlab->setText("早安,我的未婚妻");

    speech = new QTextToSpeech(this);

    tim=this->startTimer(1000);
}

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

void Widget::on_openBtn_clicked()
{
    respeech_time=this->startTimer(1000);
    //功能:启动一个定时器

    //参数:超时时间
    //返回值:当前id号
    // 创建一个QTextToSpeech对象
            QTextToSpeech *speech = new QTextToSpeech(this);

            // 设置语音输出的声音和语速
            speech->setVolume(2.0);
            speech->setRate(0.1);

            // 设置要说的文本
            QString text = "早安,我的未婚妻";

            // 语音播放文本

            respeech_time=this->startTimer(5000);
            speech->say(text);

}

void Widget::on_closeBtn_clicked()
{
      speech->stop();
       QTime sys_t=QTime::currentTime();
       QString t=sys_t.toString("hh:mm:ss");
      ui->lineEdit->setText(t);
}
//定时器事件
void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId()==tim)
    {
        QTime sys_t=QTime::currentTime();
        QString t=sys_t.toString("hh:mm:ss");
        //展示到ui界面
       ui->syslab->setText(t);

    }else if(e->timerId()==respeech_time)
    {
        QTextToSpeech *speech = new QTextToSpeech(this);

        // 设置语音输出的声音和语速
        speech->setVolume(2.0);
        speech->setRate(0.1);

        // 设置要说的文本
        QString text = "早安,我的未婚妻";

        // 语音播放文本

        respeech_time=this->startTimer(5000);
        speech->say(text);
    }
}

 main

#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值