Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Design approach to feeding model with data
Forum Updated to NodeBB v4.3 + New Features

Design approach to feeding model with data

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bergo.torino
    wrote on last edited by
    #1

    Hello,

    In my application I'm going to fetch some data from the internet. I'm sending a request (visiting special url) and as a result I get the xml file with a lot of data. I'm going to parse obtained xml to get the data I'm interested in. Because there will be more than one item obtained from xml they will be presented on list. I'm subclassing a QAbstractListModel as a base model for ListView, but... how to organize feeding the model with the data.

    I'm not really convinced if a model should do the fetching&parsing of the xml. I would rather have a separate class which would obtain all the data and would add that data to the model. Is this approach a canonical way of implementing this kind of scenarios? I'm thinking this class, lets call it Fetcher would have a pointer to the model so that it can add data to the model, I do not see here an advantage of using signals&slots to pass this data to model. The Fetcher will use separate thread for sure, so it must be a QObject so that it can be movedToThread. And again where to put QThread? I'm pretty sure it is better to have it out of the scope of Fetcher.

    Do you find any flaws in my thinking?

    Regards,
    Konrad.

    It's compiling - mostly I'm slacking off ;)

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      The approach is ok, I'd say. I would just do it the other way round: Let the model be an adapter that knows how to pull the data out of the storage class that receives the XML and parses it. The model will ping the data structure if it needs more data. You can implement some lazy loading this way.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bergo.torino
        wrote on last edited by
        #3

        That is tempting, but the xml will be fetched only once therefore the data to the model will be put only once. The QAbstractListModel has the insertRows() which suggests that the model should be feeded with the data rather than model should ask for data. On the other hand this probably been designed with the idea in mind that data to model is added by using the view (editor for example). @Volker I will think about you solution and will give it a try. I assume you have a successfull run of implementing it this way :)

        It's compiling - mostly I'm slacking off ;)

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          We implemented a tree model that lazy loads data from a database via xml requests to a server (some sort of home brewn soap).

          If you read that XML only once then it is perfectly ok to store all the data in the QStandardListModel. I'd use a separate class for the parsing and feeding the model.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bergo.torino
            wrote on last edited by
            #5

            Only once*

            *Of courts I assume that the end user will not have view presenting data from xml opened for a year or so ;) in this situation would be better to trigger reloading it from time to time ;)

            Thanks for help @Volker. I appreciate because I have not thought about the other-way-round approach.

            It's compiling - mostly I'm slacking off ;)

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              The reload button would not be hard to implement (actually I'd add a slot to the parser object). Just call "QAbstractItemModel::beginResetModel() ":http://doc.qt.nokia.com/4.7/qabstractitemmodel.html#beginResetModel before you wipe out the old data and call "QAbstractItemModel::endResetModel() ":http://doc.qt.nokia.com/4.7/qabstractitemmodel.html#endResetModel after you have repopulated it.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • B Offline
                B Offline
                bergo.torino
                wrote on last edited by
                #7

                Exactly.

                It's compiling - mostly I'm slacking off ;)

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved