Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Creating a .txt file based on the user interaction
Forum Updated to NodeBB v4.3 + New Features

Creating a .txt file based on the user interaction

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 4 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MilloMille
    wrote on last edited by
    #1

    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)

    raven-worxR 1 Reply Last reply
    0
    • M MilloMille

      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)

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @MilloMille
      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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MilloMille
        wrote on last edited by
        #3

        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?

        raven-worxR 1 Reply Last reply
        0
        • M MilloMille

          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?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • E Offline
            E Offline
            Eeli K
            wrote on last edited by
            #5

            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 http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html, especially http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html. 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.

            1 Reply Last reply
            1
            • M Offline
              M Offline
              MilloMille
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • LorenzL Offline
                LorenzL Offline
                Lorenz
                wrote on last edited by Lorenz
                #7

                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.family: "Helvetica"
                                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)
                                        fileContentArea.readCurrentFileConent()
                                    }
                                }
                            }
                
                            TextEdit {
                                id: fileContentArea
                                readOnly: true
                                text: 'Loading ....'
                                function readCurrentFileConent() {
                                    fileContentArea.text = fileUtils.readFile(Qt.resolvedUrl("newFile.txt"))
                
                                }
                                Component.onCompleted: {
                                    readCurrentFileConent()
                                }
                            }
                        }
                
                
                    }
                
                    }
                }
                

                You can read more on that on our forums
                https://v-play.net/developers/forums/t/file-inputoutput

                Cheers,
                Lorenz

                Developer @ V-Play Engine - http://v-play.net/qt

                V-Play simplifies

                • Game Development with Qt
                • Mobile App Dev with Qt esp. iOS & Android

                What others say
                V-Play scored #1 in Cross-Platform App Development Tools Report - see why: https://goo.gl/rgp3rq

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved