Including a text file in qml

  • hi, i wanted to include a text file in my qml file. the text file includes a number of jokes that i am going to display when a button is clicked. so, it means i am using a text file as a storage/database. how do i do that? please help

    • QML Store your jokes in a ListModel or an JavaScript array.
    • QML/C++ Store your jokes in a ListModel or an JavaScript array or distribute it along with your application (either as file or as a resource), parse it (using C++ code) to a QStringList and expose this list using a context property.

  • If I have 150 jokes, that means I have to store all of them in a ListModel right? This is the scenario: user clicks on button, button loads file with jokes and goes to next page. How can I include it in another qml file?

  • I would store the content externally from the qml to display it in. Perhaps you can store the jokes in a simple XML format, that you can then read using a model?

  • okay. i based my actions on your answer.

    i stored my contents inside a text file (our professor's requiring us to do so)

    i made this code

    @import QtQuick 1.0
    import 1.0

    Page {
    id: lolPage
    objectName: "lolPage"

        ListModel {
            id: jokeModel
                joke: Qt.resolvedUrl(jokes.text = TextFile.readLine())
            id: jokeDelegate
                height: 200
                            id: textJoke
                            text: joke
                            color: "black"
                            font.pixelSize: 20
        anchors.fill: parent
        model: jokeModel
        delegate: jokeDelegate

    what am i doing wrong here? i do not know how to call and display the contents of an external file using ListModel.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.