QML: Simplest way to write to a text file?
Solution 1:
There is no way to write to a file from QML/Javascript so we need a bit of C++ to do the job.
Create a new Qt Quick 2 Application (Built-in Elements)
project in Qt Creator called FileIO with the following files:
The project file: FileIO.pro
# The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += main.cpp
# Please do not modify the following two lines. Required for deployment.
include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
qtcAddDeployment()
HEADERS += fileio.h
The header file: fileio.h (inspired by Reading and writing files in QML)
#ifndef FILEIO_H
#define FILEIO_H
#include <QObject>
#include <QFile>
#include <QTextStream>
class FileIO : public QObject
{
Q_OBJECT
public slots:
bool write(const QString& source, const QString& data)
{
if (source.isEmpty())
return false;
QFile file(source);
if (!file.open(QFile::WriteOnly | QFile::Truncate))
return false;
QTextStream out(&file);
out << data;
file.close();
return true;
}
public:
FileIO() {}
};
#endif // FILEIO_H
main.cpp:
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QQmlContext>
#include "fileio.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
FileIO fileIO;
QtQuick2ApplicationViewer viewer;
viewer.rootContext()->setContextProperty("fileio", &fileIO);
viewer.setMainQmlFile(QStringLiteral("qml/FileIO/main.qml"));
viewer.showExpanded();
return app.exec();
}
And finally qml/FileIO/main.qml:
import QtQuick 2.0
Rectangle {
width: 360
height: 360
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
fileio.write("/tmp/test", "Ask Ubuntu");
Qt.quit();
}
}
}
To build the application, open a Terminal and go to the FileIO project folder to type:
`qmake && make && ./FileIO`.
It will launch the application. If you click on the MainView the text file will be created and the application closed.
Update:
The above code will work without restrictions for a desktop application. Regarding write permissions for Ubuntu Touch, you'll have to follow the guidelines about Confinement for click packages:
The application will have read/write access files in the standard XDG base directories. Specifically:
XDG_CACHE_HOME/<APP_PKGNAME> XDG_CONFIG_HOME/<APP_PKGNAME> XDG_DATA_HOME/<APP_PKGNAME> XDG_RUNTIME_DIR/confined/<APP_PKGNAME>
where APP_PKGNAME is what is used in the "name" field of the click manifest. Eg, if the click manifest has this:
$ cat ./manifest.json { "name": "com.ubuntu.developer.you.yourapp", ... }
then the app will have read/write access to these directories and any files or subdirectories under them:
XDG_CACHE_HOME/com.ubuntu.developer.you.yourapp XDG_CONFIG_HOME/com.ubuntu.developer.you.yourapp XDG_DATA_HOME/com.ubuntu.developer.you.yourapp XDG_RUNTIME_DIR/confined/com.ubuntu.developer.you.yourapp
Solution 2:
New File or Project. Choose the QML App with C++ Plugin. qmake is generally easier to pick up than cmake.
Choose a name for your project. Next.
Usually you can just choose the default framework selected. Next.
Add Bazaar Version Control, as it works well with Launchpad. You can try Git too if you are familiar with it. Launchpad supports both, but Git support is still pretty new as of July 2016.
Edit your header, adding the three lines from the comments.
Edit your source, adding just the function on the bottom.
Edit your Main.qml file, adding just the component on the bottom.
Run your application, and find the text file in your build directory. The build directory is in the same directory of your project directory, and it has the word "build-" prefixed to the beginning.