Cpp/Qt-day030919Qt

目录

完成文本编辑器的保存工作
头文件:widget.h:
#ifndef WIDGET_H
#define WIDGET_H

#include 
#include   //字体对话框
#include     //字体类
#include   //消息对话框
#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();

private slots:
    void on_fontBtn_clicked();

    void on_colorBtn_clicked();

    void on_openBtn_clicked();

    void on_saveBtn_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H
源文件:widget.c:
#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_fontBtn_clicked()
{
    bool ok;    //返回用户是否选中字体
    //直接调用getFont获取一个字体对话框
    QFont f = QFontDialog::getFont(&ok, //返回是否选择字体
                         QFont("隶书", 10, 10, false),    //初始字体
                         this,      //父组件
                         "选择字体");   //标题
    //对ok进行判断,判断用户是否选中字体了
    if(ok){
        //用户选中字体了,可以使用该字体
        //将选中的字体设置到文本文字上
        //ui->textEdit->setFont(f);     //设置所有文字的字体
        ui->textEdit->setCurrentFont(f);    //设置选中的字体
        //ui->textEdit->setFontItalic(true);  //设置字体倾斜
    }else{
        //用户取消选中字体,
        QMessageBox::information(this, "取消", "用户取消选择字体");
    }
}

void Widget::on_colorBtn_clicked()
{
    QColor c = QColorDialog::getColor(QColor("green"),  //初始颜色
                                      this,     //父组件
                                      "选择颜色");  //标题
    //判断c是否合法
    if(c.isValid()){
        //用户选择的颜色
        //将用户选择的颜色作用到文本上
        //ui->textEdit->setTextColor(c);        //设置字体颜色
        ui->textEdit->setTextBackgroundColor(c);    //设置背景颜色
    }else{
        //用户取消了选择颜色
        QMessageBox::information(this, "取消", "用户取消选择颜色");
    }
}

void Widget::on_openBtn_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,       //父组件
                                                    "选择文件",     //对话框标题
                                                    "./",           //起始路径
                                                    "All(*.*)");    //过滤器
    //判断是否选中文件
    if(fileName.isNull()){
        //用户取消了选择文件
        QMessageBox::information(this, "取消", "用户取消选择文件");
    }else{
        qDebug()<textEdit->setText(msg);
    }

}

void Widget::on_saveBtn_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this, "保存文件", "./", "All(*.*)");

    //判断用户是否选择了取消,文件名字符串为空,说明用户取消
    if(fileName.isNull()){
        //用户取消后给出提示
        QMessageBox::information(this, "取消", "用户取消选中");
    }else{
        //实例化一个文件类
        QFile file(fileName);

        if(file.exists()){
            //如果文件已存在就把他删除
            file.remove();
        }

        //以只写的方式打开文件
        if(!file.open(QIODevice::WriteOnly)){
            //打开失败
            QMessageBox::critical(this, "错误", "文件打开失败");
            return;
        }else{
            //打开成功,将框中的内容写入到文件中
            std::string s1 = ui->textEdit->toPlainText().toStdString();
            const char *text = s1.c_str();
            if(-1 == file.write(text)){
                //失败则跳出提示框
                QMessageBox::critical(this, "错误", "文件写入失败");
            }else{
                //成功也跳出提示框
                QMessageBox::information(this, "成功", "文件写入成功");
            }

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


        }
    }
}
运行结果

Cpp/Qt-day030919Qt_第1张图片

Cpp/Qt-day030919Qt_第2张图片

Cpp/Qt-day030919Qt_第3张图片

Cpp/Qt-day030919Qt_第4张图片

2.

Cpp/Qt-day030919Qt_第5张图片

头文件:widget.h:
#ifndef WIDGET_H
#define WIDGET_H

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

    void on_stopBtn_clicked();

private:
    Ui::Widget *ui;

    int timerId1;
    int timerId2;

    //定义一个播报员
    QTextToSpeech *speencher;
};
#endif // WIDGET_H
源文件:widget.c:
#include "widget.h"
#include "ui_widget.h"

#include 

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

    //默认启动一个定时器,显示当前时间
    timerId1 = this->startTimer(1000);
    //给播报员实例化空间
    speencher = new QTextToSpeech(this);
}

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


void Widget::on_startBtn_clicked()
{
    //启动一个定时器
    timerId2 = this->startTimer(1000);
    //播报
    speencher->say(ui->textEdit->toPlainText());
}

//定时器事件处理函数
void Widget::timerEvent(QTimerEvent *e){
    if(e->timerId() == timerId1){
        //说明定时器1到位,处理对应的任务
        QTime sys_t = QTime::currentTime();     //返回获取系统的时间
        //将QTime类型对象转换为字符串
        QString t = sys_t.toString("hh:mm:ss");
        ui->label->setText(t);
    }else if(e->timerId() == timerId2){
        //这是闹钟功能的定时器
        //获取返回的系统时间
        QTime sys_t = QTime::currentTime();
        //将QTime类型对象转换为字符串
        QString t = sys_t.toString("hh:mm:ss");
        //qDebug()<lineEdit->text();
        if(ui->lineEdit->text() == t){
            //当输入的时间与系统时间相同时,进行语音播报textlabel中的内容
            //播报
            speencher->say(ui->textEdit->toPlainText());
            //由于语音播报无法在截图中显示,所以,另加一个debug展示效果
            qDebug()<textEdit->toPlainText();
        }
    }
}
void Widget::on_stopBtn_clicked()
{
    //暂停闹钟的定时器
    this->killTimer(timerId2);
}
运行结果:

Cpp/Qt-day030919Qt_第6张图片

Cpp/Qt-day030919Qt_第7张图片

思维导图

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