8

QML at the moment lacks basic system commands, so I am looking for the most simple template to write to /tmp/text.txt.

3 Answers 3

5
+50

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
14
  • 1
    Are you sure that you selected New Qt Quick 2 Application, the main.cpp should be there (tested on both 13.10 and 14.04). it's Applications->Qt Quick 2 Application (Built-in Elements) for 13.10 and Applications->Qt Quick Application->Qt Quick 2 for 14.04 Apr 26, 2014 at 20:06
  • 1
    Wait a minute, it's well there in your screenshot, the second item, Qt Quick Application Apr 26, 2014 at 20:24
  • 1
    Actually, after selecting "Qt Quick Application", you have to enter the project name and path and after this step you can choose "Qt Quick2" in the drop-down list (14.04 behavior) Apr 26, 2014 at 20:36
  • 1
    You're right for the fileio.h, you need to create this one manually. Apr 26, 2014 at 20:37
  • 1
    I see, thanks for improving the list of possible solutions! Apr 30, 2014 at 9:11
4

New File or Project. Choose the QML App with C++ Plugin. qmake is generally easier to pick up than cmake. New File or Project

Choose a name for your project. Next. Choose a name for your project

Usually you can just choose the default framework selected. Next. Usually you can just choose the default framework selected.

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. Add Bazaar Version Control, as it works well with Launchpad.

Edit your header, adding the three lines from the comments. Edit your header, adding the three lines from the comments.

Edit your source, adding just the function on the bottom. Edit your source, adding just the function on the bottom.

Edit your Main.qml file, adding just the component 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. Run your application, and find the text file in your build directory.

9
  • 1
    Can you please explain the 4th step to me? You name the qml file with same name with that of the project and then import the same name in the qml file? Mind = fucked Jul 17, 2016 at 18:47
  • 1
    I majorly didn't understand importing 'foobar ' in foobar.qml Jul 17, 2016 at 19:59
  • 1
    It appears that you haven't made any change to anything since my last comment so no, sorry. Jul 17, 2016 at 20:04
  • 1
    Thanks for going so far trying to explain things to me. Unfortunately the question I've been asking is still not answered. What is the use of import Foobar 1.0? + Don't you need to register it first in main.cpp using qmlRegisterType<>() method? Jul 17, 2016 at 23:56
  • 1
    Thanks! You might want to check this out for your own knowledge; read the 2nd answer: stackoverflow.com/questions/38423590/… Jul 18, 2016 at 6:24
0

Just to clarify Confinement Policy, if you want to write a file on Ubuntu Touch, in your file mytype.cpp, replace:

QString filename = "text.txt";

by:

QString filename = "/home/phablet/.local/share/your_app_name/text.txt";

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .