• QT day3


    1、完成文本编辑器的保存工作

    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. }
    9. Widget::~Widget()
    10. {
    11. delete ui;
    12. }
    13. //字体对话框
    14. void Widget::on_pushButton_clicked()
    15. {
    16. bool ok; //判断是否选中字体
    17. QFont f= QFontDialog::getFont(&ok, QFont("宋体",10),this,"选择字体");
    18. //参数1:是否选中状态
    19. //参数2:初始字体
    20. //参数三:父组件
    21. if(ok)
    22. {
    23. //说明选择字体成功
    24. //ui->textEdit->setFont(f); 将所有字体进行改变
    25. ui->textEdit->setCurrentFont(f); //将选中的字体进行设置
    26. }
    27. else
    28. {
    29. //未选择字体
    30. QMessageBox::information(this,"提示","未选择字体");
    31. }
    32. }
    33. //颜色对话框
    34. void Widget::on_pushButton_2_clicked()
    35. {
    36. QColor c = QColorDialog::getColor(); //获取颜色
    37. //将获取的颜色放在选中的字体上
    38. //ui->textEdit->setTextColor(c); //设置字体颜色
    39. ui->textEdit->setTextBackgroundColor(c); //设置字体的背景色
    40. }
    41. //打开
    42. void Widget::on_pushButton_3_clicked()
    43. {
    44. QString fileName = QFileDialog::getOpenFileName(
    45. this,
    46. "open file",
    47. "./", //起始路径
    48. "TXT(*.txt)"
    49. );
    50. //创建文件对象,打开给定的路径下的文件
    51. QFile file(fileName);
    52. if(file.open(QFile::ReadWrite))
    53. {
    54. //打开文件,可以进行读写数据
    55. //将文件中的内容读取出来
    56. QByteArray msg = file.readAll();
    57. //将读取出来的内容放到ui界面上
    58. ui->textEdit->setText(QString::fromLocal8Bit(msg));
    59. }
    60. else
    61. {
    62. QMessageBox::information(this,"","文件打开失败");
    63. }
    64. }
    65. //另存
    66. void Widget::on_pushButton_4_clicked()
    67. {
    68. QString fileName = QFileDialog::getSaveFileName(
    69. this,
    70. "Save file",
    71. "./",
    72. "TXT(*.txt)");
    73. //创建文件对象
    74. QFile file(fileName);
    75. if(file.open(QFile::ReadWrite))
    76. {
    77. QByteArray msg =file.readAll();
    78. msg=ui->textEdit->toPlainText().toLocal8Bit();
    79. file.write(msg);
    80. }
    81. else
    82. {
    83. QMessageBox::information(this,"","文件另存失败");
    84. }
    85. }

    2、

    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. }
    9. Widget::~Widget()
    10. {
    11. delete ui;
    12. }
    13. //启动定时器按钮对应的槽函数
    14. void Widget::on_startBtn_clicked()
    15. {
    16. timer_id = this->startTimer(1000);
    17. //功能:启动一个定时器
    18. //参数:超时时间,每隔给定的时间后,自动调用定时器事件处理函数
    19. //返回值:当前定时器的id号
    20. }
    21. //关闭定时器按钮对应的槽函数
    22. void Widget::on_closeBtn_clicked()
    23. {
    24. this->killTimer(timer_id); //关闭给定的定时器
    25. }
    26. //定时器事件处理函数
    27. void Widget::timerEvent(QTimerEvent *e)
    28. {
    29. if(e->timerId() == timer_id) //说明定时器1到位
    30. {
    31. QTime sys_t = QTime::currentTime(); //获取系统时间
    32. //将QTime类对象转换为字符串
    33. QString t = sys_t.toString("hh:mm:ss");
    34. //展示到ui界面
    35. ui->timeLab->setText(t);
    36. //给播报员实例化空间
    37. speecher = new QTextToSpeech(this);
    38. if(t == ui->lineEdit->text()){
    39. speecher->say(ui->textEdit->toPlainText());
    40. }
    41. // if(e->timerId() == timer_id_1) //说明定时器1到位
    42. // {
    43. // }
    44. }
    45. }

  • 相关阅读:
    第8章 使用注解的方式整合MyBatis 入门详解
    css:两栏三栏布局
    运营人的两大核心能力
    基于下垂控制的孤岛双机并联逆变器环流抑制模型(Simulink仿真实现)
    UE5 新特性 Nanite 开启
    Qt下SVG格式图片应用
    Qt+ECharts开发笔记(一):ECharts介绍、下载和Qt调用ECharts基础柱状图Demo
    EXCEL+PYTHON学习3
    练习题59:显示实现接口
    传智书城源码+课程设计文档基于JSP+Servlet实现
  • 原文地址:https://blog.csdn.net/2302_77738263/article/details/133048309