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
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:
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.
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 header file: fileio.h (inspired by Reading and writing files in QML)
main.cpp:
And finally qml/FileIO/main.qml:
To build the application, open a Terminal and go to the FileIO project folder to type:
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:
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.
Just to clarify Confinement Policy, if you want to write a file on Ubuntu Touch, in your file mytype.cpp, replace:
by: