• C++&QT day10


    2> 完成文本编辑器的保存工作

    3>

    2:

    1. //保存按钮对应槽函数
    2. void Widget::on_saveBtn_clicked()
    3. {
    4. QString Filename=QFileDialog::getSaveFileName(this,//父组件
    5. "保存文件",
    6. "./",//起始路径
    7. "All(*.*)");
    8. //判断文件是否存在
    9. if(Filename.isNull())
    10. {
    11. QMessageBox::information(this,"提示","用户取消保存文件");
    12. return;
    13. }
    14. else
    15. {
    16. //qDebug()<
    17. //文件操作
    18. //1、实例化一个文件对象
    19. QFile file(Filename);
    20. //2、打开文件
    21. //3、读写操作
    22. //1、实例化一个文件对象
    23. file.open(QIODevice::WriteOnly);//创建文件,且权限为只写
    24. QString msg = ui->textEdit->toPlainText();//获取编辑器的文本内容
    25. QByteArray saveName;
    26. saveName.append(msg);
    27. file.write(saveName);
    28. file.close();
    29. //4、关闭文件
    30. file.close();
    31. }
    32. }

    3:

    widget.cpp

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. , ui(new Ui::Widget)
    6. {
    7. ui->setupUi(this);
    8. speecher = new QTextToSpeech(this);
    9. timer_id=this->startTimer(5);
    10. }
    11. Widget::~Widget()
    12. {
    13. delete ui;
    14. this->killTimer(timer_id);//关闭定时器
    15. }
    16. //启动按钮
    17. void Widget::on_startBtn_clicked()
    18. {
    19. flag=1;
    20. //功能:启动一个定时器
    21. //参数:超时时间,每隔给定时间后自动调用定时器事件处理函数
    22. //返回值:当前定时器的id号
    23. }
    24. //停止按钮
    25. void Widget::on_stopBtn_clicked()
    26. {
    27. speecher->stop();
    28. flag=0;
    29. }
    30. //计时器
    31. void Widget::timerEvent(QTimerEvent *e)
    32. {
    33. if(e->timerId()==timer_id)
    34. {
    35. QTime sys_t=QTime::currentTime();
    36. QString t = sys_t.toString("hh:mm:ss");
    37. ui->timelabel->setText(t);
    38. }
    39. if(flag==1)
    40. {
    41. if(ui->lineEdit->text()==ui->timelabel->text())
    42. {
    43. speecher->say(ui->textEdit->toPlainText());
    44. }
    45. }
    46. }

    widget.h

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include //定时器事件处理类
    5. #include //定时器类
    6. #include
    7. #include
    8. QT_BEGIN_NAMESPACE
    9. namespace Ui { class Widget; }
    10. QT_END_NAMESPACE
    11. class Widget : public QWidget
    12. {
    13. Q_OBJECT
    14. public:
    15. Widget(QWidget *parent = nullptr);
    16. ~Widget();
    17. void timerEvent(QTimerEvent *e) override;//定时器处理函数
    18. private slots:
    19. void on_startBtn_clicked();
    20. void on_stopBtn_clicked();
    21. private:
    22. Ui::Widget *ui;
    23. int timer_id;//定时器的id号
    24. QTextToSpeech *speecher;
    25. int flag=0;
    26. };
    27. #endif // WIDGET_H

    widget.ui

    思维导图

  • 相关阅读:
    DAY9-深度学习100例-循环神经网络(RNN)实现股票预测
    在Windows下Edge浏览器OA发起流程问题
    【KingbaseES】sys_dump命令详解及示例
    Nacos注册中心11-Server端(处理服务发现请求)
    JAVA大学生学业互助与交流平台计算机毕业设计Mybatis+系统+数据库+调试部署
    智慧环卫管理系统解决方案(垃圾分类)
    数据结构 - 逻辑结构和物理结构
    VueX的使用
    上线服务时遇到的一个SSLHandshakeException错误
    MySQL(3)
  • 原文地址:https://blog.csdn.net/m0_59031281/article/details/133046316