Qt --- Day03

<?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>
#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
#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("时间到");
}
#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值