找回密码
 立即注册
  • QQ空间
  • 回复
  • 收藏

Qt读写文件(2种方式)实现详解

admin 2019-12-15 17:52 677人围观 Qt相关

FileWR.h
    #pragma once
    #include<QtWidgets/QMainWindow>#include"ui_FileWR.h"
    classFileWR :public QMainWindow{ Q_OBJECT
    public: FileWR(QWidget *parent = Q_NULLPTR);
    private: Ui::FileWRClass ui;
    private slots:voidon_pushButton_clicked();bool FileWR::openTextByIODevice(const QString &aFileName);bool FileWR::saveTextByIODevice(const QString &aFileName);bool FileWR::saveTextByStream(const QString &aFileName);bool FileWR::openTextByStream(const QString &aFileName);};

    FileWR.cpp
      #include"FileWR.h"#include<qdir.h>#include<qfiledialog.h>#include<qmessagebox.h>#include<qtextstream.h>
      //加上这三句可解决中文乱码,但是不可以和QString::fromLocal8Bit或QStringLiteral同时使用#if _MSC_VER >= 1600#pragma execution_character_set("utf-8")#endif
      FileWR::FileWR(QWidget *parent) : QMainWindow(parent){ ui.setupUi(this);}void FileWR::on_pushButton_clicked(){//打开文件
      QString curPath = QDir::currentPath();//获取系统当前目录 QString dlgTitle = "打开一个文件"; //对话框标题 QString filter = "程序文件(*.h *.cpp);;文本文件(*.txt);;所有文件(*.*)"; //文件过滤器 QString aFileName = QFileDialog::getOpenFileName(this, dlgTitle, curPath, filter);//全路径 C:\Users\zls\source\repos\FileWR\FileWR\1.txt//QString aFileName = QFileDialog::getSaveFileName(this, dlgTitle, curPath, filter);//QMessageBox::information(this, "提示", aFileName);if (aFileName.isEmpty())return; openTextByIODevice(aFileName); saveTextByIODevice(curPath+"\\2.txt"); saveTextByIODevice(curPath + "\\3.txt"); openTextByStream(curPath + "\\4.txt");}bool FileWR::openTextByIODevice(const QString &aFileName){//用IODevice方式打开文本文件QFile aFile(aFileName);if (!aFile.exists()) //文件不存在returnfalse;if (!aFile.open(QIODevice::ReadOnly | QIODevice::Text))returnfalse; ui.textEdit->setPlainText(aFile.readAll());// .对象 ->指针 aFile.close();
      //ui->textEditDevice->setPlainText(aFile.readAll());//aFile.close();//ui->tabWidget->setCurrentIndex(0);returntrue;}bool FileWR::saveTextByIODevice(const QString &aFileName){ //用IODevice方式保存文本文件QFile aFile(aFileName);//aFile.setFileName(aFileName);if (!aFile.open(QIODevice::WriteOnly | QIODevice::Text))returnfalse; QString str = ui.textEdit->toPlainText();//整个内容作为字符串 QByteArray strBytes = str.toUtf8();//转换为字节数组//QByteArray strBytes=str.toLocal8Bit(); aFile.write(strBytes, strBytes.length()); //写入文件 aFile.close();//ui->tabWidget->setCurrentIndex(0);returntrue;}
      bool FileWR::saveTextByStream(const QString &aFileName){//用QTextStream保存文本文件QFile aFile(aFileName);if (!aFile.open(QIODevice::WriteOnly | QIODevice::Text))returnfalse;QTextStream aStream(&aFile); //用文本流读取文件 QString str = ui.textEdit->toPlainText(); //转换为字符串 aStream << str; //写入文本流 aFile.close();//关闭文件returntrue;}bool FileWR::openTextByStream(const QString &aFileName){ //用 QTextStream打开文本文件QFile aFile(aFileName);if (!aFile.exists()) //文件不存在returnfalse;if (!aFile.open(QIODevice::ReadOnly | QIODevice::Text))returnfalse;QTextStream aStream(&aFile); //用文本流读取文件 aStream.setAutoDetectUnicode(true); //自动检测Unicode,才能正常显示文档内的汉字//ui.textEdit->setPlainText(aStream.readAll()); ui.textEdit->clear();//清空 QString str;while (!aStream.atEnd()) { str = aStream.readLine();//读取文件的一行 ui.textEdit->append(str); //添加到文本框显示 } aFile.close();//关闭文件//ui->tabWidget->setCurrentIndex(1);returntrue;}


      运行结果:



      参考链接:

      http://c.biancheng.net/view/1877.html

      ----------------------------------------------------------------------------------------------------------------------
      我们尊重原创,也注重分享,文章来源于微信公众号:C Sharp开发笔记,建议关注公众号查看原文。如若侵权请联系qter@qter.org。
      ----------------------------------------------------------------------------------------------------------------------

      鲜花

      握手

      雷人

      路过

      鸡蛋

      yafeilinux和他的朋友们微信公众号二维码

      微信公众号

      专注于Qt嵌入式Linux开发等。扫一扫立即关注。

      Qt开源社区官方QQ群二维码

      QQ交流群

      欢迎加入QQ群大家庭,一起讨论学习!

      我有话说......