qt中用定时器实现闹钟功能

 

 实现的功能:1.由定时器每隔一秒发送信号,显示系统时间

                      2. 可以手动设置闹钟时间

                     3.文本编辑器为要提示的内容,当系统时间和手动设置时间相等语音播报该段文字

                    4.点击启动按钮,设置闹钟完成,并且闹钟时间不能修改

                    5.点击取消按钮,提示警告弹窗,点击yes,恢复可编辑闹钟时间。点击no,取消当前操作;

源文件

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->timer=new QTimer;
   timeid=startTimer(100);
   ui->settimelabel->setText("12 14 20:05:11");
   //连接定时器信号和自定义的槽函数
   connect(timer,&QTimer::timeout,this,&Widget::on_timer_timeout);
   //连接读文本信号和自定以槽函数
//   connect(this,&Widget::text,this,&Widget::on_text_speech);
};
void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId()==timeid)
    {
    QDateTime sysDateTime=QDateTime::currentDateTime();
    QString dateTime=sysDateTime.toString("MM dd hh:mm:ss");
    ui->systimelabel->setText(dateTime);
    }
}

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

//启动闹钟
void Widget::on_startbtn_clicked()
{
   timer->start(100);
   //闹钟启动,行编辑器和文本编辑器失效
   ui->settimelabel->setReadOnly(true);
   ui->textedit->setReadOnly(true);
}
//
void Widget::on_timer_timeout()
{
   if(ui->settimelabel->text()==ui->systimelabel->text())
   {
       QTextToSpeech *speech =new QTextToSpeech(this);
       QString text=ui->textedit->toPlainText();
       //qDebug()<<text;
       speech->say(text);
   }
};
//停止闹钟
void Widget::on_stopbtn_clicked()
{
        QMessageBox box(QMessageBox::Warning,"Warning","是否要重设闹钟",QMessageBox::Yes|QMessageBox::No);
        int ret = box.exec();
        if(ret==QMessageBox::Yes)
        {
            //关闭定时器
            timer->stop();
            //定时器关闭后行编辑器和文本编辑器重新有效
            ui->settimelabel->setReadOnly(false);
            ui->textedit->setReadOnly(false);
            //清空之前设置的行文本编辑器
            ui->settimelabel->clear();
        }
}

头文件 

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimer>
#include <QTime>
#include <QTimerEvent>
#include <QDebug>
#include <QTextToSpeech>
#include <QMessageBox>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT
signals:
//    void text();
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
  void timerEvent(QTimerEvent *event);
private slots:
  void on_startbtn_clicked();
  void on_timer_timeout();
 /* void on_text_speech()*/;
  void on_stopbtn_clicked();
private:
    Ui::Widget *ui;
    QTimer *timer;
    int timeid;

};
#endif // WIDGET_H

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值