完成文本编辑器的保存工作
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_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic: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_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic: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; //设置按钮1btn1->setParent(this);btn1->setText("启动"); //定义按钮内容btn1->resize(100,50); //设置按钮大小btn1->move(400,200); //移动按钮位置QPushButton *btn2 = new QPushButton; //设置按钮2btn2->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();
}