ListView: Dynamic number of roles



  • Hey,

    let me illustrate my situation with an example first: The user enters a SQL statement, which is being forwarded to the C++-part of my application. There it queries a database and fills the result into a model.

    Now I want to show the data to the user. Of course the best way would be some kind of table, but to keep things simple, lets say for each row I just want to print all the values for one record next to each other, which could look something like this:

    @delegate: Text {
    text: id + ", " + name + ", " + street...
    }@

    Now my problem is: I don't know how many roles there are when writing the code, because it depends on the SQL statement the user enters. In other words: I want a delegate which is able to just present all the roles in a comma seperated list, without me knowing the roles when writing the code.

    Is that possible somehow?
    Thank you!
    D.



  • Hello,
    You could for example use

    @yourDeclarativeView->rootContext()->setContextProperty("stringModel", QVariant::fromValue(stringList));@

    then use @model : stringModel@ and split if you like....



  • Hey,

    thank you very much for your answer!
    In fact, I've played around with such a solution, but it seems not to be flexible enough.

    I didn't ask for that in the first post, but as soon as I want something different than a comma seperated list, this approach fails...
    For example, if I want to achieve something like this:
    @delegate: {
    Row {
    Rectangle {
    Text { text: id }
    }
    Rectangle {
    Text { text: name }
    }
    Rectangle {
    Text { text: street }
    }
    .
    .
    .
    }
    }
    @

    So basically a row of rectangles that contain text in one row... and as many rectangles as there are roles.

    Do you have any ideas how to do something like that?
    Thank you for taking the time!



  • Hello,

    In fact such a solution is clearly a fast one, one you use if you are sure that your list won't have too many things to manage.
    As your case seems to be a more complete one, you should use a real list model in my opinion.
    Here is an excellent "how to":http://cdumez.blogspot.fr/2010/11/how-to-use-c-list-model-in-qml.html
    Good luck!


Log in to reply
 

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