9.19 QT作业

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

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;
    QFont f = QFontDialog::getFont(&ok,
                                   QFont("宋体",10,10,false),
                                   this,
                                   "选择字体");
    if(ok)
    {
        ui->textEdit->setCurrentFont(f);
    }
    else
    {
        QMessageBox::information(this,"取消","用户取消字体");
    }
}

void Widget::on_colorbtn_clicked()
{
    QColor c = QColorDialog::getColor(QColor("yellow"),
                                      this,
                                      "选择颜色");

    if(c.isValid())
    {
        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()<<filename;
        QFile file(filename);
        if(!file.isOpen())
        {
            if(!file.open(QFile::ReadWrite))
            {
                QMessageBox::critical(this,"失败","文件打开失败");
            }
        }

        QByteArray msg = file.readAll();

        file.close();

        ui->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();



        }
    }
}

main.cpp

#include "widget.h"

#include <QApplication>

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

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QObject>
#include<QTimerEvent>             //定时器事件处理类
#include<QTime>
#include<QLabel>
#include<QTextToSpeech>
#include<QTextEdit>
#include<QLineEdit>

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;         //定时器事件处理函数

signals:
     void my_sign();


private slots:
     void qidong_clicked();
     void tingzhi_clicked();

private:
    Ui::Widget *ui;

     int timer_id;             //定时器的id号


     QLabel *lab1;
     QLineEdit *edit1;
     QTextEdit *edit2;
     QTextToSpeech *speencher;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include<iostream>
#include<QObject>
#include<QDebug>
#include<QPushButton>
#include<QLineEdit>
#include<QLabel>
#include<QTextEdit>
#include<QTimerEvent>             //定时器事件处理类
#include<QTime>


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

    QPushButton *btn1 = new QPushButton;  //设置按钮1
    btn1->setParent(this);
    btn1->setText("启动");                 //定义按钮内容
    btn1->resize(100,50);                 //设置按钮大小
    btn1->move(400,200);                    //移动按钮位置

    QPushButton *btn2 = new QPushButton;  //设置按钮2
    btn2->setParent(this);
    btn2->setText("停止");                 //定义按钮内容
    btn2->resize(100,50);                 //设置按钮大小
    btn2->move(600,200);

    lab1 = new QLabel(this);            //设置标签
    lab1->resize(100,50);
    lab1->setStyleSheet("background-color:yellow;");
    //lab1->setText("");

    edit1 = new QLineEdit(this);
    edit1->resize(300,75);         //设置行编辑器大小
    edit1->move(400,80);          //移动行编辑器
    edit1->setText(QString());


    edit2 = new QTextEdit(this);
    edit2->resize(400,300);
    edit2->move(200,300);
    edit2->setText(QString("南华城里月如昼\n十二玉楼非吾乡"));

    speencher = new QTextToSpeech(this);

    connect(btn1, &QPushButton::clicked, this, &Widget::qidong_clicked);
    connect(btn2, &QPushButton::clicked, this, &Widget::tingzhi_clicked);


}

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


//启动定时器按钮对应的槽函数
void Widget::qidong_clicked()
{
    timer_id = this->startTimer(1000);
    //功能:启动一个定时器

}


//关闭定时器按钮对应的槽函数
void Widget::tingzhi_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界面
       lab1->setText(t);

        if(t == edit1->text())
        {
            speencher->say(edit2->toPlainText());
            qDebug()<<edit2->toPlainText();
        }
    }
}

main.cpp
 

#include "widget.h"

#include <QApplication>

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值