Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Data file

    QML and Qt Quick
    5
    14
    3618
    Loading More Posts
    • 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.
    • A
      Ankit.k last edited by

      I am working on Network Configuration app, so i want the data entered by the user to be stored in a file in binary format. Please help

      1 Reply Last reply Reply Quote 0
      • Q
        qxoz last edited by

        Do you need pure qml solution?

        1 Reply Last reply Reply Quote 0
        • A
          Ankit.k last edited by

          Yes. I would be developing it in QML.

          1 Reply Last reply Reply Quote 0
          • C
            chrisadams last edited by

            QtQuick 2? Use the QtQuick.LocalStorage singleton type.

            Cheers,
            Chris.

            1 Reply Last reply Reply Quote 0
            • A
              Ankit.k last edited by

              I m currently using QtQuick 1.1. Isn't there something similar in it?

              1 Reply Last reply Reply Quote 0
              • F
                favoritas37 last edited by

                Of course there is. Offline local storage using MySQL. Grab to following code and add it to a javascript file.

                storage.js
                @
                //
                /
                Key Value store (mostly used for settings storage) /
                /
                /

                /** Set value to storage */
                function setKeyValue(key, value) {
                var db = openDatabaseSync("YOUR_APP_NAME", "1.0", "Description", 10);

                db.transaction(function(tx) {
                                   tx.executeSql('CREATE TABLE IF NOT EXISTS ' +
                                                 'KeyValueStorage(keyName TEXT, textValue TEXT)');
                                   var rs = tx.executeSql("SELECT textValue FROM KeyValueStorage WHERE keyName = \"" + key + "\"");
                                   var sql = "";
                                   if(rs.rows.length>0) {
                                       sql = "UPDATE KeyValueStorage SET textValue = \"" + value + "\" WHERE keyName = \"" + key + "\"";
                                   } else {
                                       sql = "INSERT INTO KeyValueStorage(textValue, keyName) VALUES (\"" + value + "\",\"" + key + "\")";
                                   }
                                   tx.executeSql(sql);
                               });
                

                }

                /** Get value from storage */
                function getKeyValue(key) {
                var value;
                var db = openDatabaseSync("YOUR_APP_NAME", "1.0", "Description", 10);
                db.transaction(function(tx) {
                tx.executeSql('CREATE TABLE IF NOT EXISTS KeyValueStorage(keyName TEXT, textValue TEXT)');
                var result = "";
                var rs = tx.executeSql("SELECT textValue FROM KeyValueStorage WHERE keyName = "" + key + """);
                for (var i = 0; i < rs.rows.length; i++) {
                result = rs.rows.item(i).textValue;
                value = result;
                //console.log("---, " + value);
                }
                if(rs.rows.length===0) {
                value = "0";
                //console.log("---, " + value);
                }
                });
                return value;
                }
                @

                So this is a simple singleton key-value storage. To use it in a qml file just write the following:

                @
                import QtQuick 1.1
                import "storage.js" as Storage

                ...
                Storage.setKeyValue("ip", "0.0.0.0");
                ...
                @

                Regards.

                1 Reply Last reply Reply Quote 0
                • A
                  Ankit.k last edited by

                  Thanks a lot. I tried it, but where is the file location. I could not find

                  1 Reply Last reply Reply Quote 0
                  • A
                    Ankit.k last edited by

                    I managed to get a file using a Qfile. Now I want to connect the value entered by the user in the qml file to a variable whose value can be stored in the file through C++.

                    1 Reply Last reply Reply Quote 0
                    • F
                      favoritas37 last edited by

                      Wait a minute, what do you mean "where is the file location" ?

                      Why do you care where is the file stored since the functionality is working and you are either way going to access it only through your program?

                      Any way, to answer your question, there is a MySQL file created in the executable's folder in binary format that holds the data of the local database that gets created when using the code that i posted above.

                      1 Reply Last reply Reply Quote 0
                      • T
                        tzander last edited by

                        When you write you wanted a binary file, that implies to me, and I think favoritas37 that you do not ever want to use it as a document that you can email a friend.
                        The only time you want to store something in binary form is when you just want to use it to remember the data between times you run the app.

                        So thats why the mysql answer was given, its a simple and effective way to do it :)

                        If your needs are different than that, please explain, maybe there is a better solution!

                        1 Reply Last reply Reply Quote 0
                        • F
                          favoritas37 last edited by

                          Hello Thomas,

                          indeed this is what i wanted to say.

                          Thank you :)

                          1 Reply Last reply Reply Quote 0
                          • A
                            Ankit.k last edited by

                            Hi favoritas37
                            I just wanted to check whether the code was working properly or not. Thanks for your help

                            1 Reply Last reply Reply Quote 0
                            • A
                              Ankit.k last edited by

                              I would be running this app on embedded platform eventually, so will the java code make it slow

                              1 Reply Last reply Reply Quote 0
                              • T
                                tzander last edited by

                                Qt and/or QML is not using Java.

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post