qt文件读取

qt文件读取,第1张

1、Qt 作为一个通用开发库,提供了跨平台的文件 *** 作能力。文件 *** 作是应用程序必不可少的部分。

2、Qt5增加了QFileDevice类。途中所涉及的类及其用途简要说明如下:

· QFlie:访问本地文件或者嵌入资源;

· QTemporaryFile:创建和访问本地文件系统的临时文件;

· QBuffer:读写QByteArray;

· QProcess:运行外部程序,处理进程间通讯;

· QTcpSocket:TCP协议网络数据传输;

· QUdpSocket:传输 UDP 报文;

· QSslSocket:使用 SSL/TLS 传输数据;

· QFileDevice:新增加的类,提供了有关文件 *** 作的通用实现。

3、这其中,QProcess、QTcpSocket、QUdpSoctet和QSslSocket是顺序访问设备。所谓“顺序访问”,是指它们的数据只能访问一遍:从头走到尾,从第一个字节开始访问,直到最后一个字节,中途不能返回去读取上一个字节;QFile、QTemporaryFile和QBuffer是随机访问设备,可以访问任意位置任意次数,还可以使用QIODevice::seek()函数来重新定位文件访问位置指针。

4、QFile主要提供了有关文件的各种 *** 作,比如打开文件、关闭文件、刷新文件等。我们可以使用QDataStream或QTextStream类来读写文件,也可以使用QIODevice提供的read()、readLine()、readAll()以及write()这样的函数。值得注意的是,有关文件本身的信息,比如文件名、文件所在目录的名字等,则是通过QFileInfo获取,而不是自己分析文件路径字符串。

5、举个例子,打开文件时,需要参数指定打开文件的模式:

ConstantValue Description

QIODevice::NotOpen 0x0000 The device is not open.

QIODevice::ReadOnly 0x0001 The device is open for reading.

QIODevice::WriteOnly0x0002 The device is open for writing.

QIODevice::ReadWriteReadOnly | WriteOnlyThe device is open for reading and writing.

QIODevice::Append 0x0004 The device is opened in append mode, so that all data is written to the end of the file.

QIODevice::Truncate 0x0008 If possible, the device is truncated before it is opened. All earlier contents of the device are lost.

QIODevice::Text 0x0010 When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.QIODevice::Unbuffered 0x0020 Any buffer in the device is bypassed.

据我所知,QTextString这个类貌似是不存在的。倒是有个QTextStream类可以用来读取文本数据。

例如下面的函数可以读取小文本文件中的内容

void MainWindow::readFile(const QString&fpath)

{

if(fpath.isEmpty())

return

QFile file(fpath)

if(!file.open(QIODevice::ReadOnly|QIODevice::Text))

{

qDebug()<<"Open file failure!"

return

}

QTextStream fs(&file)

QString fileContent (fs.readAll())

qDebug()<<"The content of file is \n"<<fileContent

}

你需要确保你将正确的文件路径传递给了该函数,否则会导致打开文件失败。

Excel.Application 是ActiveX 控件的名字

QAxWidget::QAxWidget ( const QString &c, QWidget * parent = 0, Qt::WindowFlags f = 0 )

创建一个QAxWidget ,并且初始化ActiveX 控件C

QAxObject* workbooks = widget.querySubObject("WorkBooks")

表示从widget中获取一个com对象

这两行的意思应该是创建一个excel的QAxWidget,并且获取名为工作表的com对象


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12024994.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存