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();
}
相关推荐
原机小子44 分钟前
SpringBoot在线教育系统:从零到一的构建过程
数据库·spring boot·后端
2401_857439691 小时前
SpringBoot在线教育平台:设计与实现的深度解析
java·spring boot·后端
总是学不会.1 小时前
SpringBoot项目:前后端打包与部署(使用 Maven)
java·服务器·前端·后端·maven
IT学长编程1 小时前
计算机毕业设计 视频点播系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·视频点播系统
T0uken2 小时前
【QT Quick】C++交互:与QML类型转换
c++·qt·交互
一 乐2 小时前
英语词汇小程序小程序|英语词汇小程序系统|基于java的四六级词汇小程序设计与实现(源码+数据库+文档)
java·数据库·小程序·源码·notepad++·英语词汇
曳渔3 小时前
Java-数据结构-反射、枚举 |ू・ω・` )
java·开发语言·数据结构·算法
laocooon5238578863 小时前
java 模拟多人聊天室,服务器与客户机
java·开发语言
风槐啊3 小时前
六、Java 基础语法(下)
android·java·开发语言
苹果醋33 小时前
毕业设计_基于SpringBoot+vue的社区博客系统【源码+SQL+教程+可运行】41002
java·毕业设计·博客