【Qt-QFile-QDir】

Qt编程指南

  • [■ QDataStream](#■ QDataStream)
  • [■ QDial](#■ QDial)
  • [■ QDir](#■ QDir)
  • [■ QFile](#■ QFile)

■ QDataStream

描述QDataStream 类(数据流)QDataStream 类为QIODevice提供序列化的二进制数据。

是一个编码后的二进制流,它与操作系统等无关。

cpp 复制代码
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

cpp 复制代码
    /* 实例化对象和设置显示位置与大小 */
    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

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

■ QFile

cpp 复制代码
#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();

cpp 复制代码

cpp 复制代码
相关推荐
摇滚侠13 分钟前
JAVA 项目教程《苍穹外卖-11》,微信小程序项目,前后端分离,从开发到部署
java·开发语言·微信小程序
不会写DN18 分钟前
PHP 中处理图像的利器 GD库
开发语言·php
_MyFavorite_31 分钟前
JAVA重点基础、进阶知识及易错点总结(14)字节流 & 字符流
java·开发语言·python
羊小猪~~39 分钟前
Redis学习笔记(数据类型、持久化、事件、管道、发布订阅等)
开发语言·数据库·c++·redis·后端·学习·缓存
deep_drink40 分钟前
1.2、Python 与编程基础:文件处理与常用库
开发语言·python·elasticsearch·llm
牵牛老人44 分钟前
【QML 界面开发实战之:模块化、多QML文件调用与跨语言交互】
qt
mldlds1 小时前
使用 Qt 插件和 SQLCipher 实现 SQLite 数据库加密与解密
数据库·qt·sqlite
结衣结衣.1 小时前
【Linux】命名管道的妙用:实现进程控制与实时字符交互
linux·运维·开发语言·学习·操作系统·交互
fy121631 小时前
Java进阶——IO 流
java·开发语言·python
程序喵大人1 小时前
C++依赖关系分析:5个工具理清模块关系
开发语言·c++