首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >QML文件读写控件(预览版)

QML文件读写控件(预览版)

作者头像
Qt君
发布2019-12-25 11:18:00
6210
发布2019-12-25 11:18:00
举报
文章被收录于专栏:跟Qt君学编程跟Qt君学编程

旨在解决QML不能读写文件的问题。目前为预览版本(文末源码),供大家一起参考学习。

File组件通过source的属性来设置需要读写的文件,还可以通过访问/设置text的内容来读取/写入文件

使用

  • 注册File组件到Qml中:
qmlRegisterType<File>("MyModel", 1, 0, "File");
  • 导入File组件:
import MyModel 1.0
  • 使用:
/* 创建实例 */
File {
    id: file
    source: "D:/Document/hello.txt"
}

TextArea {
    anchors.fill: parent
    font.pixelSize: 30
    onTextChanged: file.text = text
    Component.onCompleted: text = file.text
}

源码

  • main.cpp
#include "File.h"

int main(int argc, char *argv[])
{
    ...
    qmlRegisterType<File>("MyModel", 1, 0, "File");
    ...
}
  • File.h
#ifndef QT_HUB_FILE_H
#define QT_HUB_FILE_H

#include <QObject>

class File : public QObject
{
    Q_OBJECT
public:
    File();

    Q_PROPERTY(QString source READ source WRITE setSource NOTIFY sourceChanged)
    Q_PROPERTY(QString text   READ text   WRITE setText   NOTIFY textChanged)

    QString source() const;
    void setSource(const QString &source);

    QString text() const;
    void setText(const QString &text);

signals:
    void sourceChanged();
    void textChanged();

private slots:
    void readFile();

private:
    QString m_source;
    QString m_text;
};

#endif // FILE_H
  • File.cpp
#include "File.h"

#include <QFile>
#include <QDebug>

File::File()
{
    connect(this, SIGNAL(sourceChanged()), this, SLOT(readFile()));
}

void File::setSource(const QString &source)
{
    m_source = source;
    emit sourceChanged();
}

QString File::source() const
{
    return m_source;
}

void File::setText(const QString &text)
{
    QFile file(m_source);
    if (!file.open(QIODevice::WriteOnly)) {
        m_text = "";
        qDebug() << "Error:" << m_source << "open failed!";
    }
    else {
        qint64 byte = file.write(text.toUtf8());
        if (byte != text.toUtf8().size()) {
            m_text = text.toUtf8().left(byte);
            qDebug() << "Error:" << m_source << "open failed!";
        }
        else {
            m_text = text;
        }

        file.close();
    }

    emit textChanged();
}

void File::readFile()
{
    QFile file(m_source);
    if (!file.open(QIODevice::ReadOnly)) {
        m_text = "";
        qDebug() << "Error:" << m_source << "open failed!";
    }

    m_text = file.readAll();
    emit textChanged();
}

QString File::text() const
{
    return m_text;
}
  • main.qml
...
import QtQuick 2.0
import QtQuick.Window 2.0
import QtQuick.Controls 2.0
import MyModel 1.0

Window {
    visible: true
    width: 480
    height: 320
    title: qsTr("File组件 by Qt君")

    File {
        id: file
        source: "D:/Document/hello.txt"
    }

    TextArea {
        anchors.fill: parent
        font.pixelSize: 30
        onTextChanged: file.text = text
        Component.onCompleted: text = file.text
    }
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-12-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Qt君 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用
  • 源码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档