• Qt --- Day03


    1. 1.0" encoding="UTF-8"?>
    2. 4.0">
    3. Widget
    4. 0
    5. 0
    6. <width>800width>
    7. <height>600height>
    8. font">
    9. <font>
    10. 10
    11. font>
    12. Widget
    13. 90
    14. 130
    15. <width>211width>
    16. <height>81height>
    17. 0
    18. 0
    19. font">
    20. <font>
    21. 17
    22. 75
    23. true
    24. font>
    25. background-color: rgb(255, 255, 255);
    26. Qt::AlignCenter
    27. "QLineEdit" name="timer">
    28. "geometry">
    29. 370
    30. 130
    31. 171
    32. 41
    33. "font">
    34. 13
    35. "text">
    36. "alignment">
    37. Qt::AlignCenter
    38. "QPushButton" name="start">
    39. "geometry">
    40. 370
    41. 180
    42. 70
    43. 40
    44. "font">
    45. 13
    46. 75
    47. true
    48. "text">
    49. 启动
    50. "QPushButton" name="stop">
    51. "geometry">
    52. 460
    53. 180
    54. 70
    55. 40
    56. "font">
    57. 13
    58. 75
    59. true
    60. "text">
    61. 停止
    62. "QTextEdit" name="textbox">
    63. "geometry">
    64. 90
    65. 260
    66. 451
    67. 231
    68. "font">
    69. 16
    70. "QPushButton" name="load">
    71. "geometry">
    72. 330
    73. 520
    74. 200
    75. 40
    76. "font">
    77. 14
    78. 75
    79. true
    80. "text">
    81. 保存
    82. "QPushButton" name="pushButton">
    83. "geometry">
    84. 100
    85. 520
    86. 200
    87. 40
    88. "font">
    89. 14
    90. 75
    91. true
    92. "text">
    93. 打开
    1. #include
    2. #include
    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_start_clicked();
    20. void on_stop_clicked();
    21. void on_load_clicked();
    22. void on_pushButton_clicked();
    23. private:
    24. Ui::Widget *ui;
    25. //当时时间号
    26. int time_id;
    27. //查询时间号
    28. int timer_id;
    29. //语音
    30. QTextToSpeech * speech;
    31. //文件
    32. QFile *file;
    33. };
    34. #endif // WIDGET_H
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. int i = 1;
    4. Widget::Widget(QWidget *parent)
    5. : QWidget(parent)
    6. , ui(new Ui::Widget)
    7. {
    8. ui->setupUi(this);
    9. speech = new QTextToSpeech(this);
    10. time_id = this->startTimer(1000);
    11. }
    12. Widget::~Widget()
    13. {
    14. delete ui;
    15. }
    16. void Widget::on_start_clicked()
    17. {
    18. QString t = ui->timer->text();
    19. if(t==NULL)
    20. QMessageBox::critical(this,"错误","请输入正确时间");
    21. else
    22. timer_id = this->startTimer(100);
    23. }
    24. void Widget::on_stop_clicked()
    25. {
    26. killTimer(timer_id);
    27. }
    28. void Widget::timerEvent(QTimerEvent *e)
    29. {
    30. if(e->timerId() == time_id)
    31. {
    32. QTime time = QTime::currentTime();
    33. QString s = time.toString("hh:mm:ss");
    34. ui->ltime->setText(s);
    35. }
    36. if(e->timerId() == timer_id)
    37. {
    38. if(ui->ltime->text() == ui->timer->text())
    39. {
    40. speech->say(ui->textbox->document()->toPlainText());
    41. if(i == 5)
    42. {
    43. i = 1;
    44. killTimer(timer_id);
    45. }
    46. }
    47. }
    48. }
    49. void Widget::on_load_clicked()
    50. {
    51. QString fname = QFileDialog::getSaveFileName(this,"选择文件","D:/","ALL(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");
    52. if(fname!=NULL)
    53. {
    54. //实例化一个文件对象
    55. QFile file(fname);
    56. //存储内容
    57. QString ba;
    58. //打开文件
    59. ba = ui->textbox->document()->toPlainText();
    60. if(!file.isOpen())
    61. if(!file.open(QIODevice::WriteOnly))
    62. {
    63. QMessageBox::critical(this,"","打开文件失败");
    64. return;
    65. }
    66. file.write(ba.toUtf8());
    67. file.close();
    68. }
    69. }
    70. void Widget::on_pushButton_clicked()
    71. {
    72. QString fname = QFileDialog::getOpenFileName(this,"选择文件","D:/","ALL(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");
    73. if(fname!=NULL)
    74. {
    75. //实例化一个文件对象
    76. QFile file(fname);
    77. //存储内容
    78. QByteArray ba;
    79. //打开文件
    80. if(!file.isOpen())
    81. if(!file.open(QIODevice::ReadOnly))
    82. {
    83. QMessageBox::critical(this,"","打开文件失败");
    84. return;
    85. }
    86. ba = file.readAll();
    87. file.close();
    88. ui->textbox->setText(ba);
    89. }
    90. else
    91. ui->textbox->setText("时间到");
    92. }
    1. #include "widget.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Widget w;
    7. w.show();
    8. return a.exec();
    9. }

  • 相关阅读:
    HashMap底层数据是如何存储的呢?
    CDGA数据治理工程师考试心得
    线代小课整理
    LeetCode //C - 373. Find K Pairs with Smallest Sums
    Cloneable接口与浅克隆,深克隆
    【心理学·人物】第二期(学术X综艺)
    一文了解JavaScript 中数组所有API的使用
    html转pdf(总结五种方法Java)
    JAVA计算机毕业设计家政服务公司管理信息Mybatis+系统+数据库+调试部署
    好看好玩的韩剧电视- 厄运的恋人
  • 原文地址:https://blog.csdn.net/weixin_61882921/article/details/133047346