|
- #include "communicationhistory.h"
-
- bool SaveDate(QWidget *parent, QTextEdit *edit)
- {
- QString fileName = QFileDialog::getSaveFileName(
- parent,
- "保存文本",
- QDir::homePath() + "/note.txt",
- "文本文件 (*.txt);;所有文件 (*)");
-
- if (fileName.isEmpty())
- return false;
-
- QFile file(fileName);
- if (!file.open(QIODevice::Append | QIODevice::Text))
- {
- QMessageBox::warning(parent, "错误", "无法打开文件");
- return false;
- }
-
- if (file.size() > 0)
- file.write("\n");
-
- QTextStream out(&file);
- out.setCodec("UTF-8");
- out << edit->toPlainText();
- file.close();
- return true;
- }
-
- bool ReadDate(QWidget *parent, QTextEdit *edit)
- {
- QString fileName = QFileDialog::getOpenFileName(
- parent,
- "打开文本",
- QDir::homePath(),
- "文本文件 (*.txt);;所有文件 (*)");
-
- if (fileName.isEmpty())
- return false;
-
- QFile file(fileName);
- if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
- {
- QMessageBox::warning(parent, "错误", "无法读取文件");
- return false;
- }
-
- QTextStream in(&file);
- in.setCodec("UTF-8");
- edit->setPlainText(in.readAll());
- file.close();
- return true;
- }
|