• [QT]day3


    1.一个闹钟
    widget.cpp:

    #include "widget.h"
    #include "ui_widget.h"
    
    #include 
    #include  //定时器事件处理类
    #include 
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Widget)
    {
        //给播报员实例化空间
        speecher = new QTextToSpeech(this);
        ui->setupUi(this);
        time_id=this->startTimer(1);
        flag=0;
    
    }
    
    Widget::~Widget()
    {
        delete ui;
        this->killTimer(time_id);
    }
    
    void Widget::timerEvent(QTimerEvent *e)
    {
        if(e->timerId()==time_id)  //说明定时器1到位
        {
            QTime sys_t = QTime::currentTime(); //获取系统时间
            //将QTime类对象转化为字符串
            QString t=sys_t.toString("hh:mm:ss");
            //展示到ui界面
            ui->nowtimeLab->setText(t);
    
            //语音播报脑子
            if(1==flag)
            {
                QString gotime;
                gotime=ui->hourEdit->toPlainText()+":"+ui->fengEdit->toPlainText()+":"+ui->miaoEdit->toPlainText();
                if(gotime==t)
                {
                    ssssay();
                }
            }
        }
    }
    
    void Widget::ssssay()
    {
        QString str;
        str=ui->textEdit->toPlainText();
        speecher->say(str);
    }
    
    void Widget::on_startBtn_clicked()
    {
        flag=1;
        ui->textEdit->setEnabled(false);
    }
    
    void Widget::on_cancelBtn_clicked()
    {
        speecher->stop();
        flag=0;
        ui->textEdit->setEnabled(true);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include 
    #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 *e) override;
    
        void ssssay();
    
    private slots:
        void on_startBtn_clicked();
    
        void on_cancelBtn_clicked();
    
    private:
        Ui::Widget *ui;
        int time_id;
        //定义一个播报员
        QTextToSpeech *speecher;
        int flag;
    };
    #endif // WIDGET_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    2.文件保存

    QString Filename=QFileDialog::getSaveFileName(this,//父组件
                                                      "保存文件",
                                                      "./",//起始路径
                                                      "All(*.*)");
        //判断文件是否存在
        if(Filename.isNull())
        {
            QMessageBox::information(this,"提示","用户取消保存文件");
            return;
        }
        else
        {
           //qDebug()<
           //文件操作
           //1、实例化一个文件对象
           QFile file(Filename);
           //2、打开文件
           //3、读写操作
           //1、实例化一个文件对象
           file.open(QIODevice::WriteOnly);//创建文件,且权限为只写
           QString msg = ui->textEdit->toPlainText();//获取编辑器的文本内容
           QByteArray saveName;
           saveName.append(msg);
           file.write(saveName);
           file.close();
           //4、关闭文件
           file.close();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 相关阅读:
    (附源码)node.js宠物医生预约平台 毕业设计 030945
    ClickHouse数据类型完整使用 第三章
    将Apache访问日志保存到MySQL数据库
    Excel下拉填充时,如何使得数字不递增?
    OGG-01449、OGG-01519问题处理
    Vue-1.8生命周期
    Linux make编译
    RK3588 I2C设备开发硬件检测
    AGC034E Complete Compress
    全新彩虹晴天知识付费系统/多功能系统源码/虚拟商城系统
  • 原文地址:https://blog.csdn.net/kyl_posible/article/details/133046201