如何在QML应用中读写文件
1个回答
展开全部
1)创建一个简单的模版应用
我们使用Ubuntu SDK的模版来创建一个最简单的应用:
我们选择“QML App with C++ plugin”模版来做我们的应用。
2)添加文件读写的文件到项目中
我们添加如下的C++ "FileIO类到我们的backend plugin中:
#ifndef FILEIO_H
#define FILEIO_H
#include <QObject>
#include <QTextCodec>
#include <QDebug>
class FileIO : public QObject
{
Q_OBJECT
public:
Q_PROPERTY(QString source
READ source
WRITE setSource
NOTIFY sourceChanged)
explicit FileIO(QObject *parent = 0);
Q_INVOKABLE QString read();
Q_INVOKABLE bool write(const QString& data);
QString source() { return mSource; };
public slots:
void setSource(const QString& source) { mSource = source; };
signals:
void sourceChanged(const QString& source);
void error(const QString& msg);
private:
QString getenv(const QString envVarName) const;
private:
QString mSource;
QString datapath;
};
inline QString GBK2UTF8(const QString &inStr)
{
QList<QByteArray> codecs = QTextCodec::availableCodecs();
for ( int i = 0; i < codecs.length(); i ++ ) {
// qDebug() << "codec: " + QTextCodec::codecForMib(1015)->toUnicode(codecs.at(i));
qDebug() << "codec: " << QString::fromLocal8Bit(codecs.at(i));
}
QTextCodec *gbk = QTextCodec::codecForName("GBK");
QTextCodec::setCodecForLocale(QTextCodec::codecForLocale());
// QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
QString g2u = gbk->toUnicode(gbk->fromUnicode(inStr)); // gbk convert utf8
return g2u;
}
#endif // FILEIO_H
#include "fileio.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QFileInfo>
#include <QTextCodec>
FileIO::FileIO(QObject *parent) : QObject(parent)
{
datapath = getenv("TMPDIR") + "/";
qDebug() << "datapath: " + datapath;
}
QString FileIO::read()
{
qDebug() << "reading ....!";
if (mSource.isEmpty()){
emit error("source is empty");
return QString();
}
QFile file(datapath + mSource);
QFileInfo fileInfo(file.fileName());
qDebug() << "file path: " << fileInfo.absoluteFilePath();
QString fileContent;
if ( file.open(QIODevice::ReadOnly) ) {
QString line;
QTextCodec *gbk = QTextCodec::codecForName("GBK");
QTextStream t( &file );
t.setCodec(gbk);
do {
line = t.readLine();
fileContent += line;
我们使用Ubuntu SDK的模版来创建一个最简单的应用:
我们选择“QML App with C++ plugin”模版来做我们的应用。
2)添加文件读写的文件到项目中
我们添加如下的C++ "FileIO类到我们的backend plugin中:
#ifndef FILEIO_H
#define FILEIO_H
#include <QObject>
#include <QTextCodec>
#include <QDebug>
class FileIO : public QObject
{
Q_OBJECT
public:
Q_PROPERTY(QString source
READ source
WRITE setSource
NOTIFY sourceChanged)
explicit FileIO(QObject *parent = 0);
Q_INVOKABLE QString read();
Q_INVOKABLE bool write(const QString& data);
QString source() { return mSource; };
public slots:
void setSource(const QString& source) { mSource = source; };
signals:
void sourceChanged(const QString& source);
void error(const QString& msg);
private:
QString getenv(const QString envVarName) const;
private:
QString mSource;
QString datapath;
};
inline QString GBK2UTF8(const QString &inStr)
{
QList<QByteArray> codecs = QTextCodec::availableCodecs();
for ( int i = 0; i < codecs.length(); i ++ ) {
// qDebug() << "codec: " + QTextCodec::codecForMib(1015)->toUnicode(codecs.at(i));
qDebug() << "codec: " << QString::fromLocal8Bit(codecs.at(i));
}
QTextCodec *gbk = QTextCodec::codecForName("GBK");
QTextCodec::setCodecForLocale(QTextCodec::codecForLocale());
// QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
QString g2u = gbk->toUnicode(gbk->fromUnicode(inStr)); // gbk convert utf8
return g2u;
}
#endif // FILEIO_H
#include "fileio.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QFileInfo>
#include <QTextCodec>
FileIO::FileIO(QObject *parent) : QObject(parent)
{
datapath = getenv("TMPDIR") + "/";
qDebug() << "datapath: " + datapath;
}
QString FileIO::read()
{
qDebug() << "reading ....!";
if (mSource.isEmpty()){
emit error("source is empty");
return QString();
}
QFile file(datapath + mSource);
QFileInfo fileInfo(file.fileName());
qDebug() << "file path: " << fileInfo.absoluteFilePath();
QString fileContent;
if ( file.open(QIODevice::ReadOnly) ) {
QString line;
QTextCodec *gbk = QTextCodec::codecForName("GBK");
QTextStream t( &file );
t.setCodec(gbk);
do {
line = t.readLine();
fileContent += line;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询