Qt-day3

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

cpp 复制代码
//保存按钮对应的槽函数
void Widget::on_saveBtn_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this,               //父组件
                                                    "保存文件",         //对话框标题
                                                    "./",               //起始路径
                                                    "All(*.*);;Images (*.png *.xpm *.jpg);;Text files(*.txt);;XML files(*.xml)");       //过滤器
    //判断是否选中文件
    if(fileName.isNull())   //微等同于fileName.isEmpty(),但有区别
    {
        QMessageBox::information(this, "提示", "用户取消了保存文件");
        return;
    }

    qDebug()<<fileName;         //得到文件路径

    //文件操作
    //1、实例化一个文件对象
    QFile file(fileName);

    //2、打开文件
    if(!file.isOpen())          //如果该文件没有被打开,则打开文件
    {
        //调用打开文件操作
        if(!file.open(QFile::WriteOnly))
        {
            QMessageBox::critical(this, "失败", "文件打开失败");
            return;             //文件打开失败
        }
    }

    //3、读写操作
    QString msg = ui->textEdit->toPlainText();
    file.write(msg.toLocal8Bit());

    //4、关闭文件
    file.close();

    QMessageBox::information(this, "提示", "文件保存成功");

}

2、定时器播报

头文件

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMouseEvent>
#include <QDebug>
#include <QTimerEvent>
#include <QTime>
#include <QPushButton>
#include <QTextToSpeech>
#include <QVoice>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

signals:
    void my_signal(QString msg);        //自定义一个有参无返回值信号函数

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    void timerEvent(QTimerEvent *e) override;       //定时器事件处理函数

private slots:
    //void my_slot();

    void on_startBtn_clicked();

    void on_closeBtn_clicked();


private:
    Ui::Widget *ui;

    int timer_id;       //定时器的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);

    //给播报员实例化空间
    speecher = new QTextToSpeech(this);


}

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

//启动定时器按钮对应的槽函数
void Widget::on_startBtn_clicked()
{
    //启动一个定时器
    timer_id = this->startTimer(1000);      //隔1秒
}

//关闭定时器按钮对应的槽函数
void Widget::on_closeBtn_clicked()
{
    //关闭给定的定时器
    this->killTimer(timer_id);
}

//定时器事件处理函数
void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == timer_id)            //说明定时器1到位
    {
        QTime sys_t = QTime::currentTime();
        //将QTime类对象转换为字符串
        QString t = sys_t.toString("hh:mm:ss");

        //展示到ui界面
        ui->timeLab->setText(t);

        //跟设定的时间进行对比
        if(t == ui->timeEdit->text())
        {
            //到时则进行播报
            QString text = ui->textEdit->toPlainText();
            speecher->say(text);
        }
    }


}

主程序

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

#include <QApplication>

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

ui界面

3、思维导图

相关推荐
咸鱼翻身小阿橙4 小时前
QT-P3
开发语言·qt·计算机视觉
云中飞鸿1 天前
如何编译编译 Qwt-5.2.0?
qt
雾岛听蓝1 天前
Qt 输入与多元素控件详解
开发语言·经验分享·笔记·qt
怎么没有名字注册了啊1 天前
解决qt制作软件.app迁移问题(发布)Mac
开发语言·qt
輕華2 天前
PyQt5入门实战:安装、QtDesigner设计与PyUIC转换完整指南
开发语言·qt
雾岛听蓝2 天前
Qt Widget控件属性详解
开发语言·经验分享·笔记·qt
大橘2 天前
【qml-5.1】qml与c++交互(QML_ELEMENT/QML_SINGLETON)
开发语言·c++·qt·交互·qml
雾岛听蓝2 天前
Qt按钮与标签控件详解
开发语言·经验分享·笔记·qt
碎碎念的安静2 天前
WPF 与 Qt 进程间通信(IPC)
开发语言·qt·wpf
(Charon)2 天前
【Qt/C++】Qt/C++ 中 :: 和 . 到底有什么区别?
开发语言·c++·qt