Qt --- Day03

Qt --- Day03_第1张图片



 Widget
 
  
   
    0
    0
    800
    600
   
  
  
   
    10
   
  
  
   Widget
  
  
   
    
     90
     130
     211
     81
    
   
   
    
     0
     0
    
   
   
    
     17
     75
     true
    
   
   
    background-color: rgb(255, 255, 255);
   
   
    
   
   
    Qt::AlignCenter
   
  
  
   
    
     370
     130
     171
     41
    
   
   
    
     13
    
   
   
    
   
   
    Qt::AlignCenter
   
  
  
   
    
     370
     180
     70
     40
    
   
   
    
     13
     75
     true
    
   
   
    启动
   
  
  
   
    
     460
     180
     70
     40
    
   
   
    
     13
     75
     true
    
   
   
    停止
   
  
  
   
    
     90
     260
     451
     231
    
   
   
    
     16
    
   
  
  
   
    
     330
     520
     200
     40
    
   
   
    
     14
     75
     true
    
   
   
    保存
   
  
  
   
    
     100
     520
     200
     40
    
   
   
    
     14
     75
     true
    
   
   
    打开
   
  
 
 
 

#include
#include
#include
#include
#include
#include
#include
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 

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

你可能感兴趣的:(Qt,qt,数据库,java)