Qt --- Day03

css 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="font">
   <font>
    <pointsize>10</pointsize>
   </font>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <widget class="QLabel" name="ltime">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>130</y>
     <width>211</width>
     <height>81</height>
    </rect>
   </property>
   <property name="sizePolicy">
    <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
     <horstretch>0</horstretch>
     <verstretch>0</verstretch>
    </sizepolicy>
   </property>
   <property name="font">
    <font>
     <pointsize>17</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="styleSheet">
    <string notr="true">background-color: rgb(255, 255, 255);</string>
   </property>
   <property name="text">
    <string/>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QLineEdit" name="timer">
   <property name="geometry">
    <rect>
     <x>370</x>
     <y>130</y>
     <width>171</width>
     <height>41</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>13</pointsize>
    </font>
   </property>
   <property name="text">
    <string/>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QPushButton" name="start">
   <property name="geometry">
    <rect>
     <x>370</x>
     <y>180</y>
     <width>70</width>
     <height>40</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>13</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>启动</string>
   </property>
  </widget>
  <widget class="QPushButton" name="stop">
   <property name="geometry">
    <rect>
     <x>460</x>
     <y>180</y>
     <width>70</width>
     <height>40</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>13</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>停止</string>
   </property>
  </widget>
  <widget class="QTextEdit" name="textbox">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>260</y>
     <width>451</width>
     <height>231</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>16</pointsize>
    </font>
   </property>
  </widget>
  <widget class="QPushButton" name="load">
   <property name="geometry">
    <rect>
     <x>330</x>
     <y>520</y>
     <width>200</width>
     <height>40</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>14</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>保存</string>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>100</x>
     <y>520</y>
     <width>200</width>
     <height>40</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>14</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>打开</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
cpp 复制代码
#include<QTime>
#include<QString>
#include<QFile>
#include<QMessageBox>
#include<QTextToSpeech>
#include<QDebug>
#include<QFileDialog>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void timerEvent(QTimerEvent *e) override;

private slots:
    void on_start_clicked();

    void on_stop_clicked();

    void on_load_clicked();

    void on_pushButton_clicked();

private:
    Ui::Widget *ui;

    //当时时间号
    int time_id;
    //查询时间号
    int timer_id;
    //语音
    QTextToSpeech * speech;
    //文件
    QFile *file;
};
#endif // WIDGET_H
cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
int i = 1;
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    speech = new QTextToSpeech(this);
    time_id = this->startTimer(1000);




}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_start_clicked()
{
    QString t = ui->timer->text();

    if(t==NULL)
        QMessageBox::critical(this,"错误","请输入正确时间");
    else
        timer_id = this->startTimer(100);
}

void Widget::on_stop_clicked()
{
    killTimer(timer_id);
}
void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == time_id)
    {
        QTime time = QTime::currentTime();
        QString s = time.toString("hh:mm:ss");
        ui->ltime->setText(s);
    }
    if(e->timerId() == timer_id)
    {


        if(ui->ltime->text() == ui->timer->text())
        {

            speech->say(ui->textbox->document()->toPlainText());
            if(i == 5)
            {
                i = 1;
                killTimer(timer_id);
            }
        }
    }
}

void Widget::on_load_clicked()
{
    QString fname = QFileDialog::getSaveFileName(this,"选择文件","D:/","ALL(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");
    if(fname!=NULL)
    {
        //实例化一个文件对象
        QFile file(fname);
        //存储内容
        QString ba;
        //打开文件
        ba = ui->textbox->document()->toPlainText();
        if(!file.isOpen())
            if(!file.open(QIODevice::WriteOnly))
            {
                QMessageBox::critical(this,"","打开文件失败");
                return;
            }
        file.write(ba.toUtf8());
        file.close();
    }
}

void Widget::on_pushButton_clicked()
{
    QString fname = QFileDialog::getOpenFileName(this,"选择文件","D:/","ALL(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");
    if(fname!=NULL)
    {
        //实例化一个文件对象
        QFile file(fname);
        //存储内容
        QByteArray ba;
        //打开文件
        if(!file.isOpen())
            if(!file.open(QIODevice::ReadOnly))
            {
                QMessageBox::critical(this,"","打开文件失败");
                return;
            }
        ba = file.readAll();
        file.close();

        ui->textbox->setText(ba);
    }
    else
        ui->textbox->setText("时间到");
}
cpp 复制代码
#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}
相关推荐
折哥的程序人生 · 物流技术专研8 小时前
Java面试85题图解版(一):基础核心篇
java·开发语言·后端·面试
czlczl200209258 小时前
理解 MySQL 行锁:两阶段锁协议与热点更新优化
数据库·mysql
AllData公司负责人9 小时前
通过Postgresql同步到Doris,全视角演示AllData数据中台核心功能效果,涵盖:数据入湖仓,数据同步,数据处理,数据服务,BI可视化驾驶舱
java·大数据·数据库·数据仓库·人工智能·python·postgresql
哆啦A梦15889 小时前
20, Springboot3+vue3实现前台轮播图和详情页的设计
javascript·数据库·spring boot·mybatis·vue3
Hello.Reader9 小时前
算法基础(十)——分治思想把大问题拆成小问题
java·开发语言·算法
一只大袋鼠9 小时前
JavaWeb四种文件上传方式(下篇)
java·开发语言·springmvc·javaweb
TE-茶叶蛋10 小时前
深入研究 yudao-framework 模块:Java 编程能力提升指南
java·开发语言
渣渣盟10 小时前
Mysql入门到精通全集(SQL99)包含关系运算,软考数据库工程师复习首选
数据库·mysql·oracle
逻辑驱动的ken10 小时前
Java高频考点场景题24
java·开发语言·面试·职场和发展·求职招聘
兔小盈10 小时前
多线程-(五)线程安全之内存可见性
java·开发语言·多线程