• QT实现相关功能


    1、文本的保存

    mianwindow.h

    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_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 MainWindow; }
    14. QT_END_NAMESPACE
    15. class MainWindow : public QMainWindow
    16. {
    17. Q_OBJECT
    18. public:
    19. MainWindow(QWidget *parent = nullptr);
    20. ~MainWindow();
    21. private slots:
    22. void on_pushButton_clicked();
    23. void on_pushButton_2_clicked();
    24. void on_pushButton_4_clicked();
    25. void on_pushButton_3_clicked();
    26. private:
    27. Ui::MainWindow *ui;
    28. };
    29. #endif // MAINWINDOW_H

    mainwindow.cpp

    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. MainWindow::MainWindow(QWidget *parent)
    4. : QMainWindow(parent)
    5. , ui(new Ui::MainWindow)
    6. {
    7. ui->setupUi(this);
    8. }
    9. MainWindow::~MainWindow()
    10. {
    11. delete ui;
    12. }
    13. //字体槽函数
    14. void MainWindow::on_pushButton_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. }
    31. else
    32. {
    33. //用户取消了选中字体
    34. QMessageBox::information(this, "取消", "用户取消的选择字体");
    35. }
    36. }
    37. //颜色槽函数
    38. void MainWindow::on_pushButton_2_clicked()
    39. {
    40. QColor c = QColorDialog::getColor(QColor("green"), //初始颜色
    41. this, //父组件
    42. "选择颜色"); //对话框标题
    43. //判断c的合法性
    44. if(c.isValid())
    45. {
    46. //用户选择的颜色
    47. //将用户选择的颜色作用到文本上
    48. //ui->textEdit->setTextColor(c); //设置字体颜色
    49. ui->textEdit->setTextBackgroundColor(c); //设置背景色
    50. }
    51. else
    52. {
    53. QMessageBox::information(this, "取消","用户取消了选择颜色");
    54. }
    55. }
    56. //打开按钮对应的槽函数
    57. void MainWindow::on_pushButton_4_clicked()
    58. {
    59. QString fileName = QFileDialog::getOpenFileName(this, //父组件
    60. "选择文件", //对话框标题
    61. "./", //起始路径
    62. "All(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"); //过滤器
    63. //判断是否选中文件
    64. if(fileName.isNull())
    65. {
    66. QMessageBox::information(this,"提示","用户取消了选择文件");
    67. return;
    68. }
    69. qDebug()<<fileName; //得到文件路径
    70. //文件操作
    71. //1、实例化一个文件对象
    72. QFile file(fileName);
    73. //2、打开文件
    74. if(!file.isOpen()) //如果文件没有打开
    75. {
    76. //调用打开文件操作
    77. if(!file.open(QFile::ReadWrite)) //如果打开失败就弹出对话框,不进if就是打开成功
    78. {
    79. QMessageBox::critical(this, "失败","文件打开失败");
    80. return; //文件打开失败
    81. }
    82. }
    83. //3、读写操作
    84. QByteArray msg = file.readAll();
    85. //4、关闭文件
    86. file.close();
    87. //将读取的文本展示在ui界面上
    88. ui->textEdit->setText(msg);
    89. }
    90. //保存按钮槽函数
    91. void MainWindow::on_pushButton_3_clicked()
    92. {
    93. QString fileName = QFileDialog::getSaveFileName(this, //父组件
    94. "保存文件", //对话框标题
    95. "./", //起始路径
    96. "All(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"); //过滤器
    97. //判断是否选中文件
    98. if(fileName.isNull())
    99. {
    100. QMessageBox::information(this,"提示","用户取消了选择文件");
    101. return;
    102. }
    103. qDebug()<<fileName;
    104. //保存文件操作
    105. //1、实例化一个文件对象
    106. QFile file(fileName);
    107. //2、打开文件
    108. if(!file.isOpen()) //如果文件没有打开
    109. {
    110. //调用打开文件操作
    111. if(!file.open(QFile::ReadWrite)) //如果打开失败就弹出对话框,不进if就是打开成功
    112. {
    113. QMessageBox::critical(this, "失败","文件打开失败");
    114. return; //文件打开失败
    115. }
    116. }
    117. //3、将文件保存在系统中
    118. QString str;
    119. str = ui->textEdit->toPlainText();
    120. QTextStream stream(&file);
    121. stream<<str;
    122. //4、关闭文件
    123. file.close();
    124. }

    main.cpp

    1. #include "mainwindow.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. MainWindow w;
    7. w.show();
    8. return a.exec();
    9. }

    2、闹钟

    mainwindow.h

    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. QT_BEGIN_NAMESPACE
    9. namespace Ui { class MainWindow; }
    10. QT_END_NAMESPACE
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14. public:
    15. MainWindow(QWidget *parent = nullptr);
    16. ~MainWindow();
    17. //定时器处理函数
    18. void timerEvent(QTimerEvent *e) override;
    19. private slots:
    20. void on_pushButton_clicked();
    21. void on_pushButton_2_clicked();
    22. private:
    23. Ui::MainWindow *ui;
    24. int timer_id; //定时器的id号
    25. //定义一个播报员
    26. QTextToSpeech *speecher;
    27. };
    28. #endif // MAINWINDOW_H

    mainwindow.cpp

    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. MainWindow::MainWindow(QWidget *parent)
    4. : QMainWindow(parent)
    5. , ui(new Ui::MainWindow)
    6. {
    7. ui->setupUi(this);
    8. //设定定时器
    9. timer_id = this->startTimer(1000);
    10. //在文本中添加内容
    11. ui->textEdit->setText("三更灯火五更鸡\n正是男儿读书时\n黑发不知勤学早\n白首方悔读书迟");
    12. }
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17. //启动定时器按钮对应的槽函数
    18. void MainWindow::on_pushButton_clicked()
    19. {
    20. }
    21. //关闭定时器按钮对应的槽函数
    22. void MainWindow::on_pushButton_2_clicked()
    23. {
    24. //关闭播报员
    25. speecher->stop();
    26. }
    27. //定时器事件处理函数
    28. void MainWindow::timerEvent(QTimerEvent *e)
    29. {
    30. if(e->timerId()==timer_id)
    31. {
    32. QTime sys_t=QTime::currentTime();
    33. //将QTime类对象转换为字符串
    34. QString t=sys_t.toString("hh:mm:ss");
    35. //展示ui界面
    36. ui->label->setText(t);
    37. QString T = ui->lineEdit->text();
    38. if(t==T)
    39. {
    40. //给播报员实例化空间
    41. speecher = new QTextToSpeech(this);
    42. speecher->say("三更灯火五更鸡\n正是男儿读书时\n黑发不知勤学早\n白首方悔读书迟");
    43. }
    44. }
    45. }

    main.cpp

    1. #include "mainwindow.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. MainWindow w;
    7. w.show();
    8. return a.exec();
    9. }

    3、思维导图

  • 相关阅读:
    智能语法编辑器市场现状及未来发展趋势分析
    月满中秋夜|中秋和 Jina AI 一起过
    [足式机器人]Part3机构运动微分几何学分析与综合Ch01-3 平面运动微分几何学——【读书笔记】
    图文详解Linux基础经典教程(06)——CentOS安装JDK
    C# 压缩质量可自己控制的JPEG的几种方式
    数据分析三剑客
    高德地图系列(一):vue项目如何使用高德地图、入门以及基本控件使用
    2022年暑假ACM热身练习3(详细)
    基于单片机的北斗定位无人机救火系统(两种程序:单片机与android系统app程序源码)
    动态头像如何制作?这个方法请收藏
  • 原文地址:https://blog.csdn.net/Venusler/article/details/133053988