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

in 编程
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
旨在解决QML不能读写文件的问题。目前为预览版本(文末源码),供大家一起参考学习。

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

demo.gif

使用

qmlRegisterType<File>("MyModel", 1, 0, "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
}

源码

#include "File.h"

int main(int argc, char *argv[])
{
    ...
    qmlRegisterType<File>("MyModel", 1, 0, "File");
    ...
}
#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
#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;
}
...
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
    }
}

第一时间获取最新推送,请关注微信公众号Qt君

关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看