QML FileIO 组件
FileIO 是一个常用的 QML 扩展组件,用于在 QML 中读写文件。虽然 Qt 没有内置 FileIO 组件,但开发者通常会创建自定义的 FileIO 组件或使用第三方库来实现文件操作功能。
自定义 FileIO 组件基础
基本属性
属性 | 类型 | 默认值 | 说明 |
---|---|---|---|
source | string | "" | 要操作的文件路径 |
text | string | "" | 文件内容(读写时使用) |
status | enumeration | FileIO.Null | 当前操作状态 |
errorString | string | "" | 错误描述信息 |
Status 枚举值
值 | 说明 |
---|---|
FileIO.Null | 未进行任何操作 |
FileIO.Ready | 准备就绪 |
FileIO.Reading | 正在读取 |
FileIO.Writing | 正在写入 |
FileIO.Error | 发生错误 |
常用方法
方法 | 参数 | 返回值 | 说明 |
---|---|---|---|
read() | - | string | 读取文件内容 |
write(text) | text : string | bool | 写入内容到文件 |
append(text) | text : string | bool | 追加内容到文件 |
remove() | - | bool | 删除文件 |
exists() | - | bool | 检查文件是否存在 |
copy(dest) | dest : string | bool | 复制文件到目标路径 |
rename(newName) | newName : string | bool | 重命名文件 |
常用信号
信号 | 参数 | 说明 |
---|---|---|
sourceChanged() | - | 文件路径改变时触发 |
textChanged() | - | 文件内容改变时触发 |
statusChanged() | - | 操作状态改变时触发 |
errorOccurred(message) | message : string | 发生错误时触发 |
readCompleted() | - | 读取完成时触发 |
writeCompleted() | - | 写入完成时触发 |
实现示例
C++ 后端实现 (FileIO.h)
cpp
#include <QObject>
#include <QFile>
#include <QTextStream>class FileIO : public QObject {Q_OBJECTQ_PROPERTY(QString source READ source WRITE setSource NOTIFY sourceChanged)Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)Q_PROPERTY(QString errorString READ errorString NOTIFY errorOccurred)public:explicit FileIO(QObject *parent = nullptr);QString source() const;QString text() const;QString errorString() const;Q_INVOKABLE bool read();Q_INVOKABLE bool write();Q_INVOKABLE bool append(const QString& data);Q_INVOKABLE bool remove();Q_INVOKABLE bool exists() const;public slots:void setSource(const QString& source);void setText(const QString& text);signals:void sourceChanged();void textChanged();void errorOccurred(const QString& msg);private:QString m_source;QString m_text;QString m_errorString;
};
C++ 后端实现 (FileIO.cpp)
cpp
#include "FileIO.h"FileIO::FileIO(QObject *parent) : QObject(parent) {}QString FileIO::source() const { return m_source; }
QString FileIO::text() const { return m_text; }
QString FileIO::errorString() const { return m_errorString; }void FileIO::setSource(const QString& source) {if (m_source != source) {m_source = source;emit sourceChanged();}
}void FileIO::setText(const QString& text) {if (m_text != text) {m_text = text;emit textChanged();}
}bool FileIO::read() {if (m_source.isEmpty()) {m_errorString = "Source is empty";emit errorOccurred(m_errorString);return false;}QFile file(m_source);if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {m_errorString = file.errorString();emit errorOccurred(m_errorString);return false;}QTextStream in(&file);setText(in.readAll());file.close();return true;
}bool FileIO::write() {if (m_source.isEmpty()) {m_errorString = "Source is empty";emit errorOccurred(m_errorString);return false;}QFile file(m_source);if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {m_errorString = file.errorString();emit errorOccurred(m_errorString);return false;}QTextStream out(&file);out << m_text;file.close();return true;
}bool FileIO::append(const QString& data) {if (m_source.isEmpty()) {m_errorString = "Source is empty";emit errorOccurred(m_errorString);return false;}QFile file(m_source);if (!file.open(QIODevice::Append | QIODevice::Text)) {m_errorString = file.errorString();emit errorOccurred(m_errorString);return false;}QTextStream out(&file);out << data;file.close();return true;
}bool FileIO::remove() {if (m_source.isEmpty()) {m_errorString = "Source is empty";emit errorOccurred(m_errorString);return false;}return QFile::remove(m_source);
}bool FileIO::exists() const {if (m_source.isEmpty())return false;return QFile::exists(m_source);
}
QML 使用示例
qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import MyComponents 1.0 // 注册的C++组件Column {spacing: 10FileIO {id: fileIOsource: "test.txt"onErrorOccurred: console.log("Error:", message)onReadCompleted: console.log("Read completed")onWriteCompleted: console.log("Write completed")}TextArea {id: textAreawidth: 300height: 200text: fileIO.textonTextChanged: fileIO.text = text}Row {spacing: 5Button {text: "读取文件"onClicked: fileIO.read()}Button {text: "保存文件"onClicked: fileIO.write()}Button {text: "追加内容"onClicked: fileIO.append("\nAppended text")}Button {text: "删除文件"onClicked: fileIO.remove()}Button {text: "检查存在"onClicked: console.log("文件存在:", fileIO.exists())}}
}
替代方案
如果你不想使用C++扩展,可以使用以下纯QML方案:
纯QML实现 (需要Qt.labs模块)
qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import Qt.labs.platform 1.1 as PlatformItem {function readFile(fileUrl) {var xhr = new XMLHttpRequest()xhr.open("GET", fileUrl, false)xhr.send()return xhr.status === 200 ? xhr.responseText : ""}function writeFile(fileUrl, content) {var xhr = new XMLHttpRequest()xhr.open("PUT", fileUrl, false)xhr.send(content)return xhr.status === 200 || xhr.status === 0}// 使用示例Component.onCompleted: {var content = readFile("file:///path/to/file.txt")console.log(content)writeFile("file:///path/to/file.txt", "New content")}
}
高级用法
异步文件操作
qml
import QtQuick 2.15
import QtQuick.Controls 2.15Item {function asyncReadFile(fileUrl, callback) {var xhr = new XMLHttpRequest()xhr.onreadystatechange = function() {if (xhr.readyState === XMLHttpRequest.DONE) {callback(xhr.status === 200 ? xhr.responseText : null)}}xhr.open("GET", fileUrl)xhr.send()}function asyncWriteFile(fileUrl, content, callback) {var xhr = new XMLHttpRequest()xhr.onreadystatechange = function() {if (xhr.readyState === XMLHttpRequest.DONE) {callback(xhr.status === 200 || xhr.status === 0)}}xhr.open("PUT", fileUrl)xhr.send(content)}// 使用示例Button {text: "异步读取"onClicked: {asyncReadFile("file:///path/to/file.txt", function(content) {if (content !== null) {console.log("读取成功:", content)} else {console.log("读取失败")}})}}
}
文件对话框集成
qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import Qt.labs.platform 1.1Column {spacing: 10FileIO {id: fileIO}TextArea {id: textAreawidth: 400height: 300text: fileIO.text}FileDialog {id: openDialogtitle: "打开文件"folder: StandardPaths.writableLocation(StandardPaths.DocumentsLocation)onAccepted: {fileIO.source = openDialog.filefileIO.read()}}FileDialog {id: saveDialogtitle: "保存文件"folder: StandardPaths.writableLocation(StandardPaths.DocumentsLocation)fileMode: FileDialog.SaveFileonAccepted: {fileIO.source = saveDialog.filefileIO.text = textArea.textfileIO.write()}}Row {spacing: 5Button {text: "打开"onClicked: openDialog.open()}Button {text: "保存"onClicked: saveDialog.open()}}
}
注意:实际使用时,请根据项目需求选择合适的实现方式,并注意文件系统权限问题。