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. Writing and Reading External Files
Forum Updated to NodeBB v4.3 + New Features

Writing and Reading External Files

Scheduled Pinned Locked Moved QML and Qt Quick
10 Posts 3 Posters 7.9k 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.
  • K Offline
    K Offline
    kyleplattner
    wrote on last edited by
    #1

    I am looking for a way to store preferences in an external file (probably an xml file) using QML. I would rather not use a list model since I want to able for these preferences to be read by the non-UI/non-QML parts of our application. Any help would be appreciated. Thanks.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Perhaps a custom class inserted in your QML environment would work for you? You can make a QDeclarativeItem subclass, but I think you can also just expose an existing QObject into the QDeclarative runtime environment that will then ben accessible from your QML. The actual reading and writing can then be done from C++, where you can use any file format you like, including just using QSettings.

      Edit: Perhaps QDeclarativeContext is what you need.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        syrianzoro
        wrote on last edited by
        #3

        I think you have to use javascript with your code and this is sample code for creating afile

        @
        function WriteFile()
        {

        var fh = fopen("c:\\MyFile.txt", 3); // Open the file for writing
        
        if(fh!=-1) // If the file has been successfully opened
        {
            var str = "Some text goes here...";
            fwrite(fh, str); // Write the string to a file
            fclose(fh); // Close the file
        }
        

        }

        WriteFile();
        @

        Qt is the future

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kyleplattner
          wrote on last edited by
          #4

          Syrianzoro,
          I found the same example online but it renders "fh" not recognized. If you find a way to get it to run, let me know.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            syrianzoro
            wrote on last edited by
            #5

            see samegame example I think it is what you are looking for

            @
            function sendHighScore(name) {
            var postman = new XMLHttpRequest()
            var postData = "name=" + name + "&score=" + gameCanvas.score + "&gridSize=" + maxColumn + "x" + maxRow + "&time=" + Math.floor(gameDuration / 1000);
            postman.open("POST", scoresURL, true);
            postman.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            postman.onreadystatechange = function() {
            if (postman.readyState == postman.DONE) {
            dialog.show("Your score has been uploaded.");
            }
            }
            postman.send(postData);
            }
            @

            with PHP code

            @
            <?php
            $score = $_POST["score"];
            echo "<html>";
            echo "<head><title>SameGame High Scores</title></head><body>";
            if($score > 0){#Sending in a new high score
            $name = $_POST["name"];
            $grid = $_POST["gridSize"];
            $time = $_POST["time"];
            if($name == "")
            $name = "Anonymous";
            $file = fopen("score_data.xml", "a");
            $ret = fwrite($file, "<record><score>". $score . "</score><name>"
            . $name . "</name><gridSize>" . $grid . "</gridSize><seconds>"
            . $time . "</seconds></record>\n");
            echo "Your score has been recorded. Thanks for playing!";
            if($ret == False)
            echo "<br/> There was an error though, so don't expect to see that score again.";
            }else{#Read high score list
            #Now uses XSLT to display. So just print the file. With XML cruft added.
            #Note that firefox at least won't apply the XSLT on a php file. So redirecting
            $file = fopen("scores.xml", "w");
            $ret = fwrite($file, '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n"
            . '<?xml-stylesheet type="text/xsl" href="score_style.xsl"?>' . "\n"
            . "<records>\n" . file_get_contents("score_data.xml") . "</records>\n");
            if($ret == False)
            echo "There was an internal error. Sorry.";
            else
            echo '[removed][removed].replace("scores.xml")[removed]';
            }
            echo "</body></html>";
            ?>

            @

            Qt is the future

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              syrianzoro, I think you may have misread. The OP was looking to manipulate local files. Yes, people still use those in these days of perpetual internet connections...

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kyleplattner
                wrote on last edited by
                #7

                That is true, our linux display unit will not have a constant connection.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  syrianzoro
                  wrote on last edited by
                  #8

                  [quote author="kyleplattner" date="1293027256"]I am looking for a way to store preferences in an external file (probably an xml file) using QML. [/quote]

                  sorry but I thought that you need away to write to xml file in any way. Trolls had used this way to do that. :)

                  Qt is the future

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    kyleplattner, are you bound to using an xml file? If an SQLite database would work for you too, then perhaps you should look at QML's "OfflineStorage API":http://doc.qt.nokia.com/4.7/qdeclarativeglobalobject.html.

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kyleplattner
                      wrote on last edited by
                      #10

                      I will look into it, I am up for whatever leads to simplicity and reliability.

                      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