Forums

OverviewV-Play 2 Support (Qt 5) › File input/Output

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #12556

    Andy

    Hi,

    is there any way to write to files from V-Play.

    I want to use an Android phone GPS module to write times and locations to a file (ideally to a Dropbox folder).

    Andy.

    #12570

    Lorenz

    Hi Andy,

    we have a (currently undocumented) fileUtils context property, which you can use for that.

    bool success = fileUtils.writeFile("myFile.txt", "myFileContentString")

    you can also use it to read a file as Chris mentioned in this post.

    Note that this is not officially supported as of now and you should carefully test your app if you use this!

     

    Alternatively, you can implement something that fits your needs yourself in C++. Here is a simple example on how to do it.

     

    Please let me know if that helps you or you need any further help!

    Cheers,

    Lorenz

    • This reply was modified 2 years, 9 months ago by  Lorenz.
    • This reply was modified 2 years, 9 months ago by  Lorenz.
    #12574

    Andy

    Excellent! Thanks Lorenz.

    On a quick test the fileUtils property seems to work fine for my purposes, and the stack overflow link shows where I was going wrong trying the C++ route  – I didn’t know to

    #include <QtQml>            // If you use Qt5
    
    #15156

    Davide Manuel

    Hi there.

    First of all, sorry for my english.

    I’m trying to use fileUtils.writeFile to replace a custom .json file.

    But, even if fileUtils.writeFile returns success the file isn’t changed.

    What could it be?

    #15193

    Günther
    V-Play Team

    Are you sure you are writing / overwriting the correct file? (and path)

    If you are using a relative path, the default user documents folder is prepended/used, as this ensures a writeable path on different platforms.

    Best,
    Günther

    #15194

    Davide Manuel

    Hey, Günther.

    I’m almost 100% sure that i’m using the right file/path, since i can read the same file/path.

    I read the file and after change the values i use the writeFile to overwrite it. But, nothing happens.

    In time, it’s my first v-play project and i changed the code to use Storage component and that solves my problem.

    But, i’m still looking how to fix the writeFile issue.

    Which is the default user documents folder  in the project struture?

     

    Davide.

     

     

    #15201

    Marcin

    Hi Davide,
    Just checked some simple demo for fileUtils and, it looks like, it works.

    import VPlayApps 1.0
    import QtQuick 2.0
    
    App {
        // You get free licenseKeys from http://v-play.net/licenseKey
        // With a licenseKey you can:
        //  * Publish your games & apps for the app stores
        //  * Remove the V-Play Splash Screen or set a custom one (available with the Pro Licenses)
        //  * Add plugins to monetize, analyze & improve your apps (available with the Pro Licenses)
        //licenseKey: "<generate one from http://v-play.net/licenseKey>"
    
        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()
                    }
                }
            }
    
    
        }
    
        }
    }

    Looks like, by default, Qt searches relatively from to the fiel from which you use the method.
    I created new project Qt Apps, and just changes Main.qml.
    The file I edit is in qml/newFile.txt , relatively to Main.qml which is also in qml/Main.qml
    Try this one, maybe will help you

    #15202

    Marcin

    One last thing Davide,
    Remember that Qt is executed inside your build directory, not in the place where you edit files.
    Definitely you have something like build-youProject-Desktop-Qt5.7 , or similar.
    Since the app is executed in the build direcotry, the file in build directory is being changed, not in the directory where you edit your sources.
    To see where Qt builds your app check Project(left menu)->your kit “Build”->and look for build directory option.
    The file you want to read/write will be in that directory.

    #15212

    Davide Manuel

    Hi Marcin,

    thx for the help.  You’re right. The file was in the build directory.

    Thx again.

     

Viewing 9 posts - 1 through 9 (of 9 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support