我们使用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 = 0i <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
先杀毒,看看在哪个盘里。如不在C盘,那就删掉它。如果在C盘里,实在不放心也可以删除它,大不了还原,重袜宏装告游册系统嘛!没啥大不了,但是翻译磨宏Codecs是编解码器的意思,应该没问题的!欢迎分享,转载请注明来源:内存溢出
评论列表(0条)