Approach for creating QML from structured text file



  • Hey,

    My goal is the following:

    I have a text file, which is a simple sequence of images and text, like this:

    This is some text at the beginning.
    [image=/resources/image1.png]
    This is more text.
    [image=/resources/image2.png]
    

    Loading this file should create an QML object (at runtime!) which is equivalent to this:

    Item {
        Label {
            id: text1
            text: "This is some text at the beginning."
        }
        Image {
            id: image1
            anchors.top: text1
            source: "/resources/image1.png"
        }
        Label {
            id: text2
            anchors.top: image1
            text: "This is some text at the beginning."
        }
        Image {
            id: image2
            anchors.top: text2
            source: "/resources/image2.png"
        }
    }
    

    Now, I know there are many ways to archive this. But since I am not yet very experienced with QML, I was wondering which approach you would recommend:

    • Should I do this entirely in QML? Loading the text file, parsing it and creating the objects?
    • Should I do this in C++? Can I create QML objects like this in C++?

    Thanks!
    Nathan


  • Qt Champions 2016

    Hi
    Im not using QML so I might be wrong. :)

    It seems that FILE IO must be added for QML to read files which means you
    must do it in c++ first any way

    http://stackoverflow.com/questions/8894531/reading-a-line-from-a-txt-or-csv-file-in-qml-qt-quick

    So for me the question would be if this is a import feature of the app, or
    something you will do like 1/few time and then use the result.

    if

    1. a feature of app, i would use QML for it.
    2. If one shot/not a app feature, I would make in C++ as
      file io is ready to use and not many lines of code is needed for parsing the
      file and generate the output.

    just my opinion.



  • Hey,

    Thanks for the reply @mrjj

    Can you elaborate a bit?
    What do you mean by "a feature of app" vs. "one shot/not a app feature"? Are you making a difference between if it is loaded during runtime or "pre-generated" QML files? Or if the process is initiated by the user?

    At present I could actually write a parser that generates QML files before I compile the project. But I want to stay open to the option of downloading these files from a server during runtime.

    I personally feel more like doing it in C++, but I am now sure how much of a "hassle" it is to create the QML objects in C++.


  • Qt Champions 2016

    Yes , the different use cases I mean,
    1: sort of a tool for you to create pre-generated QML that is the user app
    2: The generation is part of the user app( process is initiated by the user)

    from you post it sounds like more like tool.

    • "hassle" it is to create the QML objects in C++."

    Just to be sure. we are just talking create the QML files. like other text file.
    Not also running the QML from within c++?

    From your sample shown, it would not be huge c++ program
    You could make fast took using printf like features, like

    • QString status = QString("Processing file %1 of %2: %3") .arg(i).arg(total).arg(fileName);

    or for a more robust template system, use input text files if you need flexibility,
    so you would something like

    Item {
        Label {
            id: text1
            text: $TEXT1$
        }
        Image {
            id: image1
            anchors.top: text1
            source:$IMAGE$
        }
        Label {
            id: $TEXT2$
            anchors.top: image1
            text: $TEXT1$
        }
     ...
    }
    

    and read it line by line and replace $TOKEN$ with real values.
    Such tool would be slightly more code than the fast version.

    Using input files, makes it easier to format the output then using "printf"
    functions but it is sort of overkill if you just need the format shown and nothing else, even later.

    so I also lean towards c++ ;)


Log in to reply
 

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