• day50:QTday3,对话框补充、事件处理机制


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

    widget.h:

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include //字体对话框
    5. #include //字体类
    6. #include //消息对话框
    7. #include //信息调试类
    8. #include //颜色对话框
    9. #include //颜色类
    10. #include //文件对话框类
    11. #include //文件头文件
    12. QT_BEGIN_NAMESPACE
    13. namespace Ui { class Widget; }
    14. QT_END_NAMESPACE
    15. class Widget : public QWidget
    16. {
    17. Q_OBJECT
    18. public:
    19. Widget(QWidget *parent = nullptr);
    20. ~Widget();
    21. private slots:
    22. void on_fontBtn_clicked();
    23. void on_colorBtn_clicked();
    24. void on_openBtn_clicked();
    25. void on_saveBtn_clicked();
    26. private:
    27. Ui::Widget *ui;
    28. };
    29. #endif // WIDGET_H

    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. }
    9. Widget::~Widget()
    10. {
    11. delete ui;
    12. }
    13. //字体按钮对应的槽函数
    14. void Widget::on_fontBtn_clicked()
    15. {
    16. bool ok; //返回用户是否选中字体
    17. //直接调用getFont获取一个字体对话框
    18. QFont f = QFontDialog::getFont(&ok, //返回是否选择字体
    19. QFont("隶书", 10, 10,false), //初始字体
    20. this, //父组件
    21. "选择字体"); //对话框标题
    22. //对ok进行判断,判断用户是否选中字体了
    23. if(ok)
    24. {
    25. //用户选中字体了。可以使用该字体
    26. //将选中的字体,设置到文本文字上
    27. //ui->textEdit->setFont(f); //设置全部文字字体
    28. ui->textEdit->setCurrentFont(f); //设置选中的字体
    29. //ui->textEdit->setFontItalic(true); //设置选中字体倾斜
    30. }else
    31. {
    32. //用户取消了选中字体
    33. QMessageBox::information(this, "取消", "用户取消的选择字体");
    34. }
    35. }
    36. //颜色按钮对应的槽函数
    37. void Widget::on_colorBtn_clicked()
    38. {
    39. QColor c = QColorDialog::getColor(QColor("green"), //初始颜色
    40. this, //父组件
    41. "选择颜色"); //对话框标题
    42. //判断c的合法性
    43. if(c.isValid())
    44. {
    45. //用户选择的颜色
    46. //将用户选择的颜色作用到文本上
    47. //ui->textEdit->setTextColor(c); //设置字体颜色
    48. ui->textEdit->setTextBackgroundColor(c); //设置背景色
    49. }else
    50. {
    51. QMessageBox::information(this, "取消","用户取消了选择颜色");
    52. }
    53. }
    54. //打开按钮对应的槽函数
    55. void Widget::on_openBtn_clicked()
    56. {
    57. QString fileName = QFileDialog::getOpenFileName(this, //父组件
    58. "选择文件", //对话框标题
    59. "./", //起始路径
    60. "All(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"); //过滤器
    61. //判断是否选中文件
    62. if(fileName.isNull())
    63. {
    64. QMessageBox::information(this,"提示","用户取消了选择文件");
    65. return;
    66. }
    67. qDebug()<//得到文件路径
    68. //文件操作
    69. //1、实例化一个文件对象
    70. QFile file(fileName);
    71. //2、打开文件
    72. if(!file.isOpen()) //如果该文件没有被打开,则打开文件
    73. {
    74. //调用打开文件操作
    75. if(!file.open(QFile::ReadWrite))
    76. {
    77. QMessageBox::critical(this, "失败","文件打开失败");
    78. return; //文件打开失败
    79. }
    80. }
    81. //3、读写操作
    82. QByteArray msg = file.readAll();
    83. //4、关闭文件
    84. file.close();
    85. //将读取的文本展示在ui界面
    86. ui->textEdit->setText(msg);
    87. }
    88. //保存按钮对应的槽函数
    89. void Widget::on_saveBtn_clicked()
    90. {
    91. //获取保存文件对话框
    92. QString fileName=QFileDialog::getSaveFileName(
    93. this,"save file","./","All(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");
    94. //判断是否选中文件
    95. if(fileName.isNull())
    96. {
    97. QMessageBox::information(this,"提示","用户取消了保存文件");
    98. return;
    99. }
    100. //文件操作
    101. //1、实例化一个文件对象
    102. QFile file(fileName);
    103. //2、打开文件
    104. if(!file.isOpen()) //如果该文件没有被打开,则打开文件
    105. {
    106. //调用打开文件操作
    107. if(!file.open(QFile::ReadWrite))
    108. {
    109. QMessageBox::critical(this, "失败","文件打开失败");
    110. return; //文件打开失败
    111. }
    112. }
    113. //3、读写操作
    114. QString msg = ui->textEdit->toPlainText();
    115. file.write(msg.toUtf8());
    116. //4、关闭文件
    117. file.close();
    118. }

    成品效果图:

    二、

    hwk3.pro:

    1. QT += core gui texttospeech
    2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    3. CONFIG += c++11
    4. # The following define makes your compiler emit warnings if you use
    5. # any Qt feature that has been marked deprecated (the exact warnings
    6. # depend on your compiler). Please consult the documentation of the
    7. # deprecated API in order to know how to port your code away from it.
    8. DEFINES += QT_DEPRECATED_WARNINGS
    9. # You can also make your code fail to compile if it uses deprecated APIs.
    10. # In order to do so, uncomment the following line.
    11. # You can also select to disable deprecated APIs only up to a certain version of Qt.
    12. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
    13. SOURCES += \
    14. main.cpp \
    15. widget.cpp
    16. HEADERS += \
    17. widget.h
    18. FORMS += \
    19. widget.ui
    20. # Default rules for deployment.
    21. qnx: target.path = /tmp/$${TARGET}/bin
    22. else: unix:!android: target.path = /opt/$${TARGET}/bin
    23. !isEmpty(target.path): INSTALLS += target

    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_Btn1_clicked();
    20. void on_Btn2_clicked();
    21. private:
    22. Ui::Widget *ui;
    23. //定义一个播报员
    24. QTextToSpeech *speecher;
    25. int timer_id; //定时器id号
    26. int timer_id1; //系统定时器id号
    27. };
    28. #endif // WIDGET_H

    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. //给播报员实例化空间
    9. speecher = new QTextToSpeech(this);
    10. }
    11. Widget::~Widget()
    12. {
    13. delete ui;
    14. }
    15. //启动定时器按钮对应的槽函数
    16. void Widget::on_Btn1_clicked()
    17. {
    18. timer_id = this->startTimer(1000);
    19. timer_id1 = this->startTimer(1000);
    20. }
    21. //启动定时器按钮对应的槽函数
    22. void Widget::on_Btn2_clicked()
    23. {
    24. this->killTimer(timer_id1);
    25. }
    26. void Widget::timerEvent(QTimerEvent *e)
    27. {
    28. if(e->timerId() == timer_id)
    29. {
    30. QTime sys_t = QTime::currentTime(); //获得系统时间
    31. //将QTime类对象转换为字符串
    32. QString t1 = sys_t.toString("hh:mm:ss");
    33. //展示到ui界面
    34. ui->timeLab->setText(t1);
    35. }
    36. if(e->timerId() == timer_id1)
    37. {
    38. QTime sys_t1 = QTime::currentTime(); //获得系统时间
    39. //将QTime类对象转换为字符串
    40. QString t1 = sys_t1.toString("hh:mm:ss");
    41. if(t1==ui->lineEdit->text())
    42. {
    43. speecher->say(ui->textEdit->toPlainText());
    44. }
    45. }
    46. }

    效果图:

    三、思维导图:day2对话框补充、 有道云笔记

                            day3事件处理机制、有道云笔记

  • 相关阅读:
    以太坊源码笔记-blockchain
    2022年全国大学生数学建模竞赛E题目-小批量物料生产安排详解+思路+Python代码时序预测模型(二)
    input控件的maxlength属性
    拦截器以及统一功能的实现
    求每个店铺访问次数top3的访客信息
    算法|每日一题|只出现一次的数字|位运算
    限制条件加入构造范围:Gym - 102832L
    Docker 创建网络
    Socks5代理与网络安全:保护您的隐私与数据
    POD创建与删除简单描述
  • 原文地址:https://blog.csdn.net/wxmchong/article/details/133049697