Qtday3

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

 .cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

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

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


void MainWindow::on_fontBtn_clicked()
{
    bool ok;//返回用户是否选中字体
    QFont f=QFontDialog::getFont(&ok,QFont("隶书",10,10,false),
                                 this,"选择字体");
    //对ok进行判断,判断用户是否选中字体了
    if(ok)
    {
        //用户选中字体了
        //将选中的字体,设置到文字上
        //ui->textEdit->setFont(f);//设置全部字体文字
        ui->textEdit->setCurrentFont(f);//设置选中的字体字体


    }
    else
    {
        //用户取消了选中字体
        QMessageBox::information(this,"取消","用户取消的选择字体");

    }
}
//颜色对话框对应的槽函数
void MainWindow::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 MainWindow::on_openBtn_clicked()
{
    QString fileName =QFileDialog::getOpenFileName(this,
                                                   "选择文件",
                                                   "./",
                                                   "All(*.*);;Image(*.png *.xpm *.jpg);; "
                                                   "Text files (*.txt);;files(*.xml)");
    //判断是否选中文件
    if(fileName.isNull())
    {
        QMessageBox::information(this,"提示","用户取消了选中文件");
        return;
    }
    qDebug()<<fileName;//得到文件路径
    //文件操作
    //1.实例化
    QFile file(fileName);
    //打开文件
    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 MainWindow::on_saveBtn_clicked()
{

    QString fileName = QFileDialog::getSaveFileName(this,                     //父组件
                                                        "保存文件",                //对话框标题
                                                        "./",                     //起始路径
                                                        "all file(*.*);;Text(*.txt);;Image(*.png,*.jpg,*.gif)");   //过滤器

    //判断是否选中文件
    if(fileName.isNull())
    {
        QMessageBox::information(this,"提示","用户取消了选中文件");
        return;
    }
    qDebug()<<fileName;//得到文件路径
    //文件操作
    //1.实例化
    QFile file(fileName);
    //2.打开文件
    if(!file.open(QFile::Append))//该文件没有被打开则打开文件
    {

        return;
    }
    //3.获取textEdit中的内容
    QString msg=ui->textEdit->toPlainText();
    //4.textEdit将内容写入
    file.write(msg.toLocal8Bit());
    //关闭文件
    file.close();
}

 .h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QWidget>
#include<QFont>
#include<QMessageBox>
#include<QFontDialog>
#include<QColor>//属于颜色类
#include<QColorDialog>
#include<QFileDialog>
#include<QFile>
#include<QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_fontBtn_clicked();

    void on_colorBtn_clicked();

    void on_openBtn_clicked();

    void on_saveBtn_clicked();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

 

 

 .cpp

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->timer=new QTimer;
    timeid=startTimer(100);

   
    //尺寸
    this->setFixedSize(800,600);
    //更改标题
    this->setWindowTitle("闹钟");
    //连接定时器信号和自定义的槽函数

    connect(timer,&QTimer::timeout,this,&Widget::LCD_Show);
}

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

//启动的槽函数
void Widget::on_upBtn_clicked()
{
    timer->start(100);
    ui->pushtime->setReadOnly(true);
    ui->textt->setReadOnly(true);

}
void Widget::LCD_Show()
{
    if(ui->pushtime->text()==ui->nowtime->text())
       {
           speech =new QTextToSpeech(this);
           QString text=ui->textt->toPlainText();
           //qDebug()<<text;
           speech->say(text);
       }

}

//定时器事件处理函数
void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId()==timeid)
    {
        QDateTime sysDateTime=QDateTime::currentDateTime();
        //讲QTime类对象转化为字符串
        QString t=sysDateTime.toString("hh:mm:ss");
        //展示到ui界面
        ui->nowtime->setText(t);
    }

}
//停止的槽函数
void Widget::on_downBtn_clicked()
{
    QMessageBox box(QMessageBox::Warning,"Warning","是否要重设闹钟",QMessageBox::Yes|QMessageBox::No);
           int ret = box.exec();
           if(ret==QMessageBox::Yes)
           {
               //关闭定时器
               timer->stop();
               //定时器关闭后行编辑器和文本编辑器重新有效
               ui->pushtime->setReadOnly(false);
               ui->textt->setReadOnly(false);
               //清空之前设置的行文本编辑器
               ui->pushtime->clear();
               //停止语音播报
               speech->stop();


           }

}

.h

#ifndef WIDGET_H
#define WIDGET_H


#include <QWidget>
#include <QTimer>
#include <QTime>
#include <QTimerEvent>
#include <QDebug>
#include <QTextToSpeech>
#include <QMessageBox>


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 *event)override;
    //void paintEvent(QPaintEvent *event) override;

private slots:
    void on_upBtn_clicked();

    void on_downBtn_clicked();


    void LCD_Show();
private:
    Ui::Widget *ui;


    QTimer *timer;//显示屏显示时间

    int timeid;//定义闹钟定时器
    //定义文本输入框的播报者
      QTextToSpeech *speech;

};
#endif // WIDGET_H

.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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值