这方面的应用,主要用来读写配置文件、写生产数据文件这些方面。
也算是经常用到的功能了。


代码:
#include "QtForOpencv.h"
#include <opencv2\opencv.hpp>
#include <opencv2\core.hpp>
#include <opencv2\imgcodecs.hpp>
#include <opencv2\imgproc.hpp>
#include <QFileDialog>
#include <qmessagebox.h>
#include <QTextCodec>
#include <qdatetime.h>
#include <QTextStream>
using namespace cv;
using namespace std;
QtForOpencv::QtForOpencv(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
connect(ui.btnOpenFile, &QPushButton::clicked, [=]{
QString path = QFileDialog::getOpenFileName(this, "打开文件", "E:\\");
if (path.isEmpty()){
QMessageBox::warning(this, "警告", "打开失败");
}
else
{
ui.lineEditPath->setText(path);
QFile file(path);
file.open(QFileDevice::ReadOnly);
QByteArray ary;
ary = file.readAll();
ui.textEditMsg->setText(ary);
file.close();
//转换编码
file.open(QFileDevice::ReadOnly);
QTextCodec* codec = QTextCodec::codecForName("gbk");
while (!file.atEnd()){
ary += file.readLine();
}
ui.textEditMsg->append(codec->toUnicode(ary));
file.close();
//写文件
file.open(QFileDevice::Append);
file.write("this is a test");
file.close();
//获取文件信息
QFileInfo info(path);
qDebug() << "路径:" << info.filePath() << "名称:" <<
info.fileName() << "文件大小" << info.size() <<
"后缀名:" << info.suffix();
qDebug() << "创建日期: " << info.created().toString("yyyy-MM-dd hh:mm:ss");
qDebug() << "修改日期: " << info.lastModified().toString("yyyy-MM-dd hh:mm:ss");
//文件流
//分为: 文本流(基础数据类型)和数据流(大型QImage)
//文本流
QFile file("1.txt");
file.open(QFileDevice::WriteOnly);
QTextStream stream(&file);
stream << QString("hellow qt!") << 12345;
file.close();
file.open(QFileDevice::ReadOnly);
QString str;
str = stream.readAll();
qDebug() << str;
//数据流 二进制
QFile file("2.txt");
file.open(QFileDevice::WriteOnly);
QDataStream stream(&file);
stream << QString("hellow qt!") << 2019;
file.close();
file.open(QFileDevice::ReadOnly);
QString str;
int num;
stream >> str >> num;
qDebug() << str << num;
}
});
waitKey(0);
}在我们保存生产数据的时候,最常用的是保存为csv文件,即逗号分隔的文本文件。
这种扩展名的格式,在excel读取时,可以很好的分列显示。但是这种csv文件格式仍然是一种文本文件,仅是扩展名为csv,而不是txt。
下一篇,勇哥想研究一下非常重要的话题: 多线程、多线程中更新ui界面。
这个话题是能真正用QT来进行开发的关键,没有哪一个程序不会用到它。
---------------------
作者:hackpig
来源:www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!
本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:



少有人走的路


















