QT-day3

完成文本编辑器的保存工作

void Widget::on_savebton_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this,
                                                    "保存",
                                                    "./",
                                                    "All(*.*);;Images(*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");

    QFile file(fileName);
    if(!file.isOpen()){
        if(!file.open(QFile::ReadWrite)){

            QMessageBox::critical(this,"失败","文件保存失败");

        }
    }
    QString msg = ui->textEdit->toPlainText();
    file.write((msg.toUtf8()));
    file.close();

}

QT-day3_第1张图片

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#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);

private slots:
    void on_startbtn_clicked();

    void on_stopbtn_clicked();

private:
    Ui::Widget *ui;
    int timer_id;
    int timer_id2;
    QTextToSpeech *speecher;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    speecher = new QTextToSpeech(this);
    ui->lineEdit->setText("10秒读一次");

}

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


void Widget::on_startbtn_clicked()
{
    timer_id = this->startTimer(1000);
    timer_id2 = this->startTimer(10000);
}

void Widget::on_stopbtn_clicked()
{
    this->killTimer(timer_id);
    this->killTimer(timer_id2);
}

void Widget::timerEvent(QTimerEvent *e)
{
   if(e->timerId() == timer_id){
       QTime sys_t = QTime::currentTime();
       QString t = sys_t.toString("hh:mm:ss");
       ui->timelab->setText(t);
   }
   if(e->timerId() == timer_id2){
       speecher->say(ui->textEdit->toPlainText());
   }


}

测试结果

QT-day3_第2张图片

思维导图

QT-day3_第3张图片

QT-day3_第4张图片

你可能感兴趣的:(qt,开发语言)