【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 复制代码
相关推荐
小龙Guo10 分钟前
QT+opencv实现卡尺工具找圆、拟合圆
开发语言·qt·opencv
XQ丶YTY21 分钟前
大二java第一面小厂(挂)
java·开发语言·笔记·学习·面试
一只努力学习的Cat.2 小时前
C++:二叉搜索树
开发语言·c++
<但凡.2 小时前
C++修炼:多态
开发语言·c++·算法
我爱写代码?2 小时前
Spark 集群配置、启动与监控指南
大数据·开发语言·jvm·spark·mapreduce
买了一束花2 小时前
数据预处理之数据平滑处理详解
开发语言·人工智能·算法·matlab
秭霏鱼2 小时前
Python+大模型 day01
开发语言·python
破晓的历程2 小时前
Qt之Qfile类
开发语言·qt
纸包鱼最好吃3 小时前
java基础-package关键字、MVC、import关键字
java·开发语言·mvc
njsgcs3 小时前
opencascade.js stp vite webpack 调试笔记
开发语言·前端·javascript