Important: Please read the Qt Code of Conduct -

Writing and Reading External Files

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

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

  • 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



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

  • 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);"POST", scoresURL, true);
    postman.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    postman.onreadystatechange = function() {
    if (postman.readyState == postman.DONE) {"Your score has been uploaded.");

    with PHP code

    $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.";
    echo '[removed][removed].replace("scores.xml")[removed]';
    echo "</body></html>";


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

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

  • [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. :)

  • 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":

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

Log in to reply