Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and writing files in QML (Qt)

Tags:

qt

qml

I am trying to implement Reading and writing files in QML and came across the linked article from Nokia but have not been able to successfully use the seemingly obvious code example. I think I need not say I am a total newbie in this field.

Where do I place this code snippet (which is the second code snippet on the page. ):

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

I also keep getting an error with regard to qmlRegisterType not being registered in the context when I place the above code snippet in my main form. Can someone please offer some advice on how to implement this (or any method to read and write files in QML / Qt)?

like image 620
Nepaluz Avatar asked Jul 26 '13 13:07

Nepaluz


People also ask

How do I read a text file in QML?

You can read local text files using XMLHttpRequest in QML.

How do you read and write in Qt?

Using Streams to Read Files By default, it assumes that the file is encoded in UTF-8. This can be changed using QTextStream::setEncoding(). To write text, we can use operator<<(), which is overloaded to take a QTextStream on the left and various data types (including QString) on the right: QFile file("out.

What is difference between Qt and QML?

QML is the language; its JavaScript runtime is the custom V4 engine, since Qt 5.2; and Qt Quick is the 2D scene graph and the UI framework based on it. These are all part of the Qt Declarative module, while the technology is no longer called Qt Declarative.


1 Answers

If your files is text only, you can use XMLHttpRequest (both for reading and writing), like this:

function openFile(fileUrl) {
    var request = new XMLHttpRequest();
    request.open("GET", fileUrl, false);
    request.send(null);
    return request.responseText;
}

function saveFile(fileUrl, text) {
    var request = new XMLHttpRequest();
    request.open("PUT", fileUrl, false);
    request.send(text);
    return request.status;
}

Here is demo app (Qt 5.6):

import QtQuick 2.6
import QtQuick.Dialogs 1.2
import QtQuick.Controls 1.5

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Demo App")

    function openFile(fileUrl) {
        var request = new XMLHttpRequest();
        request.open("GET", fileUrl, false);
        request.send(null);
        return request.responseText;
    }

    function saveFile(fileUrl, text) {
        var request = new XMLHttpRequest();
        request.open("PUT", fileUrl, false);
        request.send(text);
        return request.status;
    }

    FileDialog {
        id: openFileDialog
        nameFilters: ["Text files (*.txt)", "All files (*)"]
        onAccepted: textEdit.text = openFile(openFileDialog.fileUrl)
    }

    FileDialog {
        id: saveFileDialog
        selectExisting: false
        nameFilters: ["Text files (*.txt)", "All files (*)"]
        onAccepted: saveFile(saveFileDialog.fileUrl, textEdit.text)
    }

    menuBar: MenuBar {
        Menu {
            title: qsTr("File")
            MenuItem {
                text: qsTr("&Open")
                onTriggered: openFileDialog.open()
            }
            MenuItem {
                text: qsTr("&Save")
                onTriggered: saveFileDialog.open()
            }
            MenuItem {
                text: qsTr("Exit")
                onTriggered: Qt.quit();
            }
        }
    }

    TextArea {
        id: textEdit
        anchors.fill: parent
        text:
            "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " +
            "sed do eiusmod tempor incididunt ut labore et dolore magna " +
            "aliqua. Ut enim ad minim veniam, quis nostrud exercitation " +
            "ullamco laboris nisi ut aliquip ex ea commodo cosnsequat. ";
    }
}

P.S. Note that all modern browsers will throw security exception if you will try use functions like above, but QML allow it (even for file rewrites). Not sure is it by design or mistake, though.

like image 74
evg656e Avatar answered Sep 28 '22 05:09

evg656e