Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 com.nokia.symbian 1.0

    Page {
    id: lolPage
    objectName: "lolPage"

        ListModel {
            id: jokeModel
            ListElement{
                joke: Qt.resolvedUrl(jokes.text = TextFile.readLine())
            }
        Component{
            id: jokeDelegate
            ListItem{
                height: 200
                    Column{
                       Text{
                            id: textJoke
                            text: joke
                            color: "black"
                            font.pixelSize: 20
                            }
                        }
    
                    }
                }
    ListView{
        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