Creating a .txt file based on the user interaction

  • Hello,
    I'm right in the middle of designing a UI with Qt Quick for a Touch Screen. In the end, I want a UI that creates a .txt file which is then red by another programm. I still have no idea how to achieve this and I am starting to wonder it Qt Widget would have been a better choice? Of course I would be glad if I don't have to start over so I would prefer to keep my Qt Quick project.
    Does anybody have an idea how to achieve this?

    For anybody interessted as to what exactly I want to achieve:
    I'm designing a GUI for a 3D Printer I build. I the user to be able to chose between different model that he wants to print. Qt reads and edits the .txt file and feds it to another programm (Pronterface) which will generate the movement based on the code. Somehow I have to creates the .txt file and find out how to trasnfer it to pronterface as well as to start the print in the other software (Pronterface is based on python)

  • Moderators

    pure QML isn't capable to read/write files. You need to register your custom QObject C++ class (registered to the QML engine) to call your methods and do the work there.

  • Can you explain how that would work? I sthere an example soemwhere? I'm sorry but I'm totally new to Qt. I also thought about using the shared memory class to transfer the data. Do you think I should switch to Qt Widget?

  • Moderators

    @MilloMille said in Creating a .txt file based on the user interaction:

    Do you think I should switch to Qt Widget?

    no need to.
    You just need to expose your custom QObject class (which handles the file writing) to QML. Then you can call it's methods on it like any other JavaScript object.

    Read this for more informations.

  • Basically QML handles mouse/touch/keyboard input and draws graphical output to screen. Other I/O and communication with the underlying system has to be done with C++. Read, especially Use QFile and QProcess in your C++ code.

    Write QObject-based classes which handle files and external processes. Declare Q_PROPERTYs, public slots, signals and Q_INVOKABLEs for communication with QML. In you main() use for example

    QQmlEngine engine;
    Message msg;
    engine.rootContext()->setContextProperty("msg", &msg);

    as shown in the documentation. Then all your QML code recogizes the object with name 'msg' and you can use it in QML as a normal object, namely the aforementioned properties, slots, signals and invokables. Maybe it's possible even to create and use QFile and QProcess objects directly without wrapper classes.

    At first it requires a bit more work than using plain QML, but in the end you get better architecture with "business logic" and low level stuff in C++ backend and UI in QML frontend.

  • Thanks a lot for your help guys!! I think will get it to work now

  • Hi,

    If you don't mind importing V-Play into your project, you could also make use of the fileUtils that are built into V-Play. It could save you from hassling around with C++ yourself.

    Here is a simple example application that reads and writes files using V-Play:

    import VPlayApps 1.0
    import QtQuick 2.0
    App {
        NavigationStack {
        Page {
            title: qsTr("Main Page")
            Column {
                width: parent.width
                TextEdit {
                    id: editableArea
                    width: parent.width
                    height: 300
                    text: "Hello, start typing here ..."
                    font.pointSize: 20
                    color: "blue"
                    focus: true
                Rectangle {
                    color: 'blue'
                    width: 150
                    height: 50
                    radius: 25
                    x: parent.x / 2
                    Text {
                        text: 'Save current text'
                        anchors.centerIn: parent
                        color: '#fff'
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            fileUtils.writeFile(Qt.resolvedUrl("newFile.txt"), editableArea.text)
                TextEdit {
                    id: fileContentArea
                    readOnly: true
                    text: 'Loading ....'
                    function readCurrentFileConent() {
                        fileContentArea.text = fileUtils.readFile(Qt.resolvedUrl("newFile.txt"))
                    Component.onCompleted: {

    You can read more on that on our forums


Log in to reply