23062QTday3

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

cpp 复制代码
//保存文件
void Widget::on_savebtn_clicked()
{
    QString newfile=QFileDialog::getSaveFileName(this,
                                                 "保存文件",
                                                 "./",
                                                 "All(*.*);;Images(*.png *.xpm *.jpg);;Text files(*.txt);;");
    if(newfile.isNull())
    {
        QMessageBox::information(this,"提示","用户取消了保存文件");
        return;
    }
    QFile file(newfile);
    if (!file.open(QIODevice:: Truncate| QIODevice::ReadWrite| QIODevice::Text))
             return;
    QString msg=ui->textEdit->toPlainText();
    file.write(QByteArray(msg.toUtf8()));
    file.close();
}

运行结果:

头文件

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QTimerEvent>
#include<QTime>
#include <QTextToSpeech>          //文本转语音类
#include<QVoice>
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_startbtn_clicked();

    void on_closebtn_clicked();

private:
    Ui::Widget *ui;
    int timer_id;
    //定义一个播报员
    QTextToSpeech *speecher;
};
#endif // WIDGET_H

源文件:

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

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


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

}

void Widget::on_closebtn_clicked()
{
  //  this->killTimer(timer_id);
    speecher->stop();
    ui->lineEdit->clear();//清空闹钟定时
}

void Widget::timerEvent(QTimerEvent *e)
{
    //给播报员实例化空间
    speecher = new QTextToSpeech(this);
    if(e->timerId()==timer_id)
    {
        QTime sys_t=QTime::currentTime();
        QString t=sys_t.toString("hh:mm:ss");
        ui->label->setText(t);
        if(t==ui->lineEdit->text())
        speecher->say(ui->textEdit->toPlainText());
        speecher->rate();

    }
}

测试文件:

cpp 复制代码
#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}
相关推荐
EndingCoder10 分钟前
Next.js 中间件:自定义请求处理
开发语言·前端·javascript·react.js·中间件·全栈·next.js
FirstFrost --sy10 分钟前
C++ stack and queue
开发语言·c++·queue·stack·priority_queue
墨城之左11 分钟前
低版本 IntelliJ IDEA 使用高版本 JDK 语言特性的问题
java·开发语言·intellij-idea·jdk21
别来无恙1491 小时前
Java Web开发:Session与Cookie详细入门指南
java·开发语言
华阙之梦1 小时前
QT环境搭建
开发语言·qt
GalaxyPokemon1 小时前
Linux的pthread怎么实现的?(包括到汇编层的实现)
运维·开发语言·c++
lsx2024062 小时前
Ruby 条件判断
开发语言
臻实2 小时前
Win10系统Ruby+Devkit3.4.5-1安装
开发语言·后端·ruby
IT毕设实战小研2 小时前
Java毕业设计选题推荐 |基于SpringBoot的水产养殖管理系统 智能水产养殖监测系统 水产养殖小程序
java·开发语言·vue.js·spring boot·毕业设计·课程设计
Gu_shiwww2 小时前
数据结构3线性表——单链表(C)
c语言·开发语言·数据结构