day3_QT

day3_QT

  • 1、文件保存
  • 2、始终事件 -闹钟

1、文件保存

day3_QT_第1张图片

2、始终事件 -闹钟

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 



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 *event) override;

private slots:
    void on_startBtn_clicked();

    void on_overBtn_clicked();

private:
    Ui::Widget *ui;

    int timer_id_show;  //定时器的id号

    int timer_id_start;

    QString inputTime;

    QTextToSpeech *speecher;


};
#endif // WIDGET_H

widget.c

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    //启动时的定时器时间  用于左上时间的显示
    timer_id_show = this->startTimer(1000);

    speecher = new QTextToSpeech(this);

    ui->setupUi(this);
}


//定时器事件处理
void Widget::timerEvent(QTimerEvent *event)
{
    QString timeStr;
    if(event->timerId() == timer_id_show)
    {
        /*ui界面优化*/
        ui->timeLabel->setAlignment(Qt::AlignCenter);
        //启动程序时候,就要显示表
        QTime sys_t = QTime::currentTime();
        timeStr =  sys_t.toString("hh:mm:ss");
        ui->timeLabel->setText(timeStr);

    }
    if(event->timerId() == timer_id_start)
    {
        //处理时间相同否?
        if(inputTime == timeStr )
        {
            //时间到
            QString str = ui->text->toPlainText();
            speecher->say(str);
        }

    }
}


void Widget::on_startBtn_clicked()
{
    inputTime = ui->inTime->text();
    timer_id_start = this->startTimer(1000);
}


void Widget::on_overBtn_clicked()
{
    this->killTimer(timer_id_start);
    ui->inTime->clear();
}

Widget::~Widget()
{
    this->killTimer(timer_id_show);
    delete ui;
}

你可能感兴趣的:(learn,qt,开发语言)