• Qt(day3)


    思维导图

    小练习 

    second.h

    1. #ifndef SECOND_H
    2. #define SECOND_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. namespace Ui {
    12. class Second;
    13. }
    14. class Second : public QWidget
    15. {
    16. Q_OBJECT
    17. public slots:
    18. void jump_slot();
    19. void startbtn_slot();
    20. void endbtn_slot();
    21. public:
    22. explicit Second(QWidget *parent = nullptr);
    23. ~Second();
    24. void timerEvent(QTimerEvent *event) override;
    25. private:
    26. Ui::Second *ui;
    27. QLabel *lab;
    28. QLineEdit *ledit;
    29. QPushButton *startbtn;
    30. QPushButton *endbtn;
    31. QTextEdit *tedit;
    32. int timer_id; //定时器id
    33. QTextToSpeech *speecher;
    34. };
    35. #endif // SECOND_H

    second.cpp

    1. #include "second.h"
    2. #include "ui_second.h"
    3. Second::Second(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::Second)
    6. {
    7. ui->setupUi(this);
    8. speecher =new QTextToSpeech(this); //为播报员实例化空间
    9. this->setFixedSize(QSize(800,700)); //固定文件框的大小
    10. this->setWindowTitle("定时器"); //设置文件的标题
    11. lab=new QLabel(this); //创建一个lab
    12. lab->resize(300,220); //设置lab大小
    13. lab->move(50,50); //设置lab的位置
    14. lab->setStyleSheet("background-color:skyblue;font-weight:bold;font-size:60px;");
    15. ledit=new QLineEdit(this); //创建一个ledit
    16. ledit->resize(300,100); //设置ledit大小
    17. ledit->move(lab->x()+400,lab->y()); //设置ledit的位置
    18. startbtn=new QPushButton(this); //创建一个startbtn
    19. startbtn->resize(140,100); //设置startbtn大小
    20. startbtn->move(ledit->x(),ledit->y()+120); //设置startbtn的位置
    21. startbtn->setText("start"); //设置startbtn的文本
    22. endbtn=new QPushButton(this); //创建一个endbtn
    23. endbtn->resize(140,100); //设置endbtn大小
    24. endbtn->move(ledit->x()+160,ledit->y()+120); //设置endbtn的位置
    25. endbtn->setText("end"); //设置endbtn的文本
    26. tedit=new QTextEdit(this); //创建一个tedit
    27. tedit->resize(700,300);
    28. tedit->move(lab->x(),lab->y()+300);
    29. connect(startbtn,&QPushButton::clicked,this,&Second::startbtn_slot);
    30. connect(endbtn,&QPushButton::clicked,this,&Second::endbtn_slot);
    31. }
    32. Second::~Second()
    33. {
    34. delete ui;
    35. }
    36. void Second::jump_slot(){
    37. this->show();
    38. }
    39. void Second::startbtn_slot(){
    40. timer_id=this->startTimer(1000);
    41. }
    42. void Second::endbtn_slot(){
    43. this->killTimer(timer_id);
    44. }
    45. void Second::timerEvent(QTimerEvent *event){
    46. QTime sys_t=QTime::currentTime();
    47. QString t=sys_t.toString("hh:mm:ss");
    48. if(event->timerId()==timer_id){
    49. lab->setText(t);
    50. }
    51. if(t==ledit->text()){
    52. speecher->say(tedit->toPlainText());
    53. }
    54. }

     效果图:

    文本编辑器的保存工作

    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

    weight.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. //直接调用静态成员函数调出字体对话框
    18. QFont f = QFontDialog::getFont(&ok, //返回是否选中字体
    19. QFont("隶书", 2, 10, false), //初始字体
    20. this, //父组件
    21. "字体对话框"); //对话框标题
    22. //要对ok进行判断,如果用户点击的取消,则ok为false,否则为true
    23. if(ok)
    24. {
    25. //说明用户选中了字体,直接使用字体即可
    26. //将该字体加载到文本上面
    27. //ui->textEdit->setFont(f); //将该组件中的全部字体进行设置新字体
    28. ui->textEdit->setCurrentFont(f); //将组件中当前选中的字体进行设置,其余字体不设置
    29. }else
    30. {
    31. //说明用户没有选中字体
    32. QMessageBox::information(this,"提示","用户没有选中字体");
    33. }
    34. }
    35. //颜色按钮对应的槽函数
    36. void Widget::on_colorBtn_clicked()
    37. {
    38. QColor c = QColorDialog::getColor(QColor(0,255,255), //初始颜色
    39. this, //父组件
    40. "选择颜色"); //窗口标题
    41. //有没有选中颜色
    42. if(c.isValid())
    43. {
    44. //说明该颜色合肥,直接使用
    45. //ui->textEdit->setTextColor(c); //设置字体的颜色,前景色
    46. ui->textEdit->setTextBackgroundColor(c); //设置字体背景色
    47. }else
    48. {
    49. //说明颜色没有选中
    50. QMessageBox::information(this,"提示","用户没有选中颜色");
    51. }
    52. }
    53. //打开文件按钮对应的槽函数
    54. void Widget::on_openBtn_clicked()
    55. {
    56. QString fileName = QFileDialog::getOpenFileName(this, //父组件
    57. "选中文件", //对话框标题
    58. "./", //起始路径
    59. "all(*.*);; Img(*.png *.jpg *.xpm);; 文本(*.txt)");//过滤器
    60. qDebug()<<fileName;
    61. //文件操作
    62. //1、实例化一个文件对象
    63. QFile file(fileName); //使用有参构造构造一个文件对象
    64. if(!file.exists()) //判断文件是否存在
    65. {
    66. return;
    67. }
    68. //2、打开文件
    69. if(!file.open(QFile::ReadWrite)) //判断文件是否打开成功
    70. {
    71. return;
    72. }
    73. //3、从文件中读取数据
    74. QByteArray msg = file.readAll();
    75. //4、关闭文件
    76. file.close();
    77. //将从文件中读取的内容展示到ui界面上
    78. ui->textEdit->setText(QString::fromLocal8Bit(msg));
    79. }
    80. //保存按钮对应的槽函数
    81. void Widget::on_saveBtn_clicked()
    82. {
    83. //获取路径
    84. QString fileName = QFileDialog::getSaveFileName();
    85. //实例化文件对象
    86. QFile file(fileName);
    87. //打开文件
    88. if(!file.open(QFile::WriteOnly))
    89. {
    90. return;
    91. }
    92. //将ui界面上的数据读取下来
    93. QString msg = ui->textEdit->toPlainText();
    94. //将信息写入到文件中
    95. file.write(msg.toLocal8Bit());
    96. //关闭文件
    97. file.close();
    98. }

    效果图:

     

  • 相关阅读:
    oracle如果不适用toad或者plsql工具如何获取索引建表语句
    电力系统机组组合优化调度(IEEE14节点、IEEE30节点、IEEE118节点)(Matlab代码实现)
    Chapter001-FPGA学习之Vivado的LED闪烁
    loongarch集成preempt rt后ltpstress死机的问题分析
    瑞斯康达raisecom交换机基础配置
    世界杯海信再出圈,三星:“谈不上愉悦”
    Python计算机二级基本操作题和简单应用题
    Lapce:一个开发中的快速、轻量级的开源代码编辑器
    【正点原子STM32连载】 第二十五章 TFTLCD(MCU屏)实验 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
    安装opencv-python
  • 原文地址:https://blog.csdn.net/qq_53268516/article/details/133041681