加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡阳站长网 (https://www.0734zz.cn/)- 数据集成、设备管理、备份、数据加密、智能搜索!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

从qml中的.txt或.csv文件中读取一行(Qt Quick)

发布时间:2020-12-30 23:42:47 所属栏目:资源 来源:网络整理
导读:我需要在模拟屏幕上显示一个字符串.为此,我应该从现有的Filename.txt / Filename.csv文件中读取文本. text参数更新,如下面的代码所示.我需要从文本文件中访问该字符串并在MarqueeText元素中使用它. Accessed字符串应在MarqueeText元素的文本字段中使用. Marq

我需要在模拟屏幕上显示一个字符串.为此,我应该从现有的Filename.txt / Filename.csv文件中读取文本. text参数更新,如下面的代码所示.我需要从文本文件中访问该字符串并在MarqueeText元素中使用它. Accessed字符串应在MarqueeText元素的文本字段中使用.

MarqueeText {
     id:scrolltext
     width: 255
     height: 48
     anchors.verticalCenter: parent.horizontalCenter
     text:   //i need to access the string in text file to be displayed
}

请帮我解决一下这个.谢谢.

解决方法

按照Wiki页面阅读有关使用QML访问文件的信息.诺基亚维基论坛 http://web.archive.org/web/20150227025348/http://developer.nokia.com/community/wiki/Reading_and_writing_files_in_QML

摘要:

创建自定义QML类型FileIO:

fileio.h

#ifndef FILEIO_H
#define FILEIO_H

#include <QObject>

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 mSource;
};

#endif // FILEIO_H

fileio.cpp

#include "fileio.h"
#include <QFile>
#include <QTextStream>

FileIO::FileIO(QObject *parent) :
    QObject(parent)
{

}

QString FileIO::read()
{
    if (mSource.isEmpty()){
        emit error("source is empty");
        return QString();
    }

    QFile file(mSource);
    QString fileContent;
    if ( file.open(QIODevice::ReadOnly) ) {
        QString line;
        QTextStream t( &file );
        do {
            line = t.readLine();
            fileContent += line;
         } while (!line.isNull());

        file.close();
    } else {
        emit error("Unable to open the file");
        return QString();
    }
    return fileContent;
}

bool FileIO::write(const QString& data)
{
    if (mSource.isEmpty())
        return false;

    QFile file(mSource);
    if (!file.open(QFile::WriteOnly | QFile::Truncate))
        return false;

    QTextStream out(&file);
    out << data;

    file.close();

    return true;
}

注册新的QML类型:

#include "fileio.h"

Q_DECL_EXPORT int main(int argc,char *argv[])
{
   ...
    qmlRegisterType<FileIO,1>("FileIO",1,"FileIO");
    ...
}

实际QML用法:

import QtQuick 1.1
import FileIO 1.0

Rectangle {
    width: 360
    height: 360
    Text {
        id: myText
        text: "Hello World"
        anchors.centerIn: parent
    }

    FileIO {
        id: myFile
        source: "my_file.txt"
        onError: console.log(msg)
    }

    Component.onCompleted: {
        console.log( "WRITE"+ myFile.write("TEST"));
        myText.text =  myFile.read();
    }
}

(编辑:衡阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读