day50:QTday3,对话框补充、事件处理机制

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

widget.h:

#ifndef WIDGET_H
#define WIDGET_H


#include <QWidget>
#include<QFontDialog>            //字体对话框
#include<QFont>                   //字体类
#include<QMessageBox>               //消息对话框
#include<QDebug>                    //信息调试类
#include<QColorDialog>              //颜色对话框
#include<QColor>                     //颜色类
#include<QFileDialog>                //文件对话框类
#include<QFile>                      //文件头文件


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.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_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(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");    //过滤器
    //判断是否选中文件
    if(fileName.isNull())
    {
        QMessageBox::information(this,"提示","用户取消了选择文件");
        return;
    }


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


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


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


    //3、读写操作
    QByteArray msg = file.readAll();


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


    //将读取的文本展示在ui界面
    ui->textEdit->setText(msg);


}

//保存按钮对应的槽函数
void Widget::on_saveBtn_clicked()
{
    //获取保存文件对话框
    QString fileName=QFileDialog::getSaveFileName(
            this,"save file","./","All(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");

    //判断是否选中文件
    if(fileName.isNull())
    {
        QMessageBox::information(this,"提示","用户取消了保存文件");
        return;
    }

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


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

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

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

成品效果图:

二、

hwk3.pro:

QT       += core gui texttospeech

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    widget.cpp

HEADERS += \
    widget.h

FORMS += \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

widget.h:

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

    void on_Btn2_clicked();

private:
    Ui::Widget *ui;

    //定义一个播报员
    QTextToSpeech *speecher;

    int timer_id;    //定时器id号
    int timer_id1;   //系统定时器id号
};
#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);
}

Widget::~Widget()
{
    delete ui;

}

//启动定时器按钮对应的槽函数
void Widget::on_Btn1_clicked()
{
    timer_id = this->startTimer(1000);
    timer_id1 = this->startTimer(1000);
}

//启动定时器按钮对应的槽函数
void Widget::on_Btn2_clicked()
{
    this->killTimer(timer_id1);
}

void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == timer_id)
    {
        QTime sys_t = QTime::currentTime();     //获得系统时间
        //将QTime类对象转换为字符串
        QString t1 = sys_t.toString("hh:mm:ss");

        //展示到ui界面
        ui->timeLab->setText(t1);
    }
    if(e->timerId() == timer_id1)
    {
        QTime sys_t1 = QTime::currentTime();     //获得系统时间
        //将QTime类对象转换为字符串
        QString t1 = sys_t1.toString("hh:mm:ss");

        if(t1==ui->lineEdit->text())
        {
            speecher->say(ui->textEdit->toPlainText());
        }
    }
}

效果图:

三、思维导图:day2对话框补充、 有道云笔记

                        day3事件处理机制、有道云笔记

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值