当前位置: 首页 > news >正文

【Qt-QFile-QDir】

Qt编程指南

  • ■ Stream
    • ■ QTextStream
    • ■ QDataStream
  • ■ QDial
  • ■ QDir
  • ■ QFile

■ Stream

■ QTextStream

/* 获取文件的路径 */
QString fileName = QFileDialog::getOpenFileName(this);/* 指向文件 */
file.setFileName(fileName);/* 判断文件是否存在 */
if (!file.exists())return false;/* 以读写的方式打开 */
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))return false;/* 使用文本流读取文件 */
QTextStream stream(&file);
stream.setCodec("utf-8"); //这行的目的是支持读取中文信息  // stream.setCodec("GB2312");/* 读取文本到textEdit */
textEdit->setPlainText(stream.readAll());
/* 关闭文件 */
file.close();/* 以只写的方式打开 */
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))return;
/* 用文本流读取文件 */
QTextStream stream(&file);
/* 获取textEdit的文本内容,转为字符串 */
QString str = textEdit->toPlainText();
/* 使用流提取运算符,写入文本流 */
stream<<str;
/* 关闭文件 */
file.close();

■ QDataStream

描述QDataStream 类(数据流)QDataStream 类为QIODevice提供序列化的二进制数据。
是一个编码后的二进制流,它与操作系统等无关。

QFile fileModify(filePath);
if(!fileModify.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)){     qDebug() << "cart.par file open failed!";     return;
}
QDataStream dataStream(&fileModify);
QVector<int> numVec;
for(int i=0; i<100; i++){      numVec.push_back(i);
}
for(auto n : mumVec){      dataStream << n << "      "
}
fileModify.close();QTextStream QTextStream 类(文本流)
QTextStream 类:用于对数据进行文本格式的读/写操作,可在 QString、QIODevice或 QByteArray 上运行,比如把数据输出到 QString、QIODevice 或 QByteArray 对象上,或进行相反的操作。QFile cartFile(filePath);
if(!cartFile.open(QIODevice::ReadWrite | QIODevice::Text))
{     qDebug() << "Open cart.part file failed!";     return;
}
QStringList fileContent;
QTextStream readWrite(&cartFile);
QString line = readWrite.readLine();
fileContent << line;
cartFile.close();

■ QDial

    /* 实例化对象和设置显示位置与大小 */dial = new QDial(this);dial->setGeometry(300, 100, 200, 200);/* 设置页长(两个最大刻度的间距)*/dial->setPageStep(10);/* 设置刻度可见 */dial->setNotchesVisible(true);/* 设置两个凹槽之间的目标像素数 */dial->setNotchTarget(1.00);/* 设置dial值的范围 */dial->setRange(0,100);/* 开启后可指向圆的任何角度 *///dial->setWrapping(true);/* 实例化对象和设置显示位置与大小 */label = new QLabel(this);label->setGeometry(370, 300, 200, 50);/* 初始化为0km/h */label->setText("0km/h");/* 信号槽连接 */connect(dial, SIGNAL(valueChanged(int)),this, SLOT(dialValueChanged(int)));
}MainWindow::~MainWindow()
{
}void MainWindow::dialValueChanged(int val)
{/* QString::number()转换成字符串 */label->setText(QString::number(val) + "km/h");
}

■ QDir

/转\(斜杠转反斜杠)
QDir::toNativeSeparators接口
QString path = "C:/temp/test.txt";
path = QDir::toNativeSeparators(path);
\转/(反斜杠转斜杠)
QString path = "C:\\temp\\test.txt";
path = QDir::fromNativeSeparators(path);

■ QFile

#include <QFile>
/* QFile类型对象 */
QFile file;
/* 获取文件的路径 */
QString fileName = QFileDialog::getOpenFileName(this);
/* 指向文件 */
file.setFileName(fileName);
/* 判断文件是否存在 */
if (!file.exists())return false;/* 以读写的方式打开 */
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))return false;/* 读取文本到textEdit */
textEdit->setPlainText(file.readAll());
/* 关闭文件 */
file.close();
/* 以只读的方式打开 */
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))return;/* 转换为字节数组 */
QByteArray strBytes = str.toUtf8();
/* 写入文件 */
file.write(strBytes, strBytes.length());
/* 关闭文件 */
file.close();



http://www.lryc.cn/news/267456.html

相关文章:

  • 设计模式之-单列设计模式,5种单例设计模式使用场景以及它们的优缺点
  • Android 13 - Media框架(25)- OMXNodeInstance(二)
  • 生物系统学中的进化树构建和分析R工具包V.PhyloMaker2的介绍和详细使用
  • XStream 反序列化漏洞 CVE-2021-39144 已亲自复现
  • 深入剖析LinkedList:揭秘底层原理
  • 计算机网络复习-OSI TCP/IP 物理层
  • 虚拟机服务器中了lockbit2.0/3.0勒索病毒怎么处理,数据恢复应对步骤
  • 【MATLAB】 RGB和YCbCr互转
  • 【线性代数】决定张成空间的最少向量线性无关吗?
  • 暴力破解(Pikachu)
  • 如何使用CMake查看opencv封装好的函数
  • 微盛·企微管家:用户运营API集成,电商无代码解决方案
  • Hive 部署
  • CopyOnWriteArrayList源码阅读
  • Windows操作系统:共享文件夹,防火墙的设置
  • STM32独立看门狗
  • 财务数据智能化:用AI工具高效制作财务分析PPT报告
  • vue3中使用three.js记录
  • MySQL——表的内外连接
  • 基于IPP-FFT的线性调频Z(Chirp-Z,CZT)的C++类库封装并导出为dll(固定接口支持更新)
  • 【C语言】指针
  • PostgreSql 索引使用技巧
  • 【华为数据之道学习笔记】6-7打造业务自助分析的关键能力
  • K8S从harbor中拉取镜像的规则imagePullPolicy
  • LeetCode刷题--- 优美的排列
  • 关于edge浏览器以及插件推荐【亲测好用】
  • 关于“Python”的核心知识点整理大全43
  • Android Framework一些问题思考
  • 2024年安全员-C证证考试题库及安全员-C证试题解析
  • 推广主要指标及定义