Results 1 to 9 of 9

Thread: [QT4]QTreeView, QAbstractItemModel and a MySQL database

  1. #1
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default [QT4]QTreeView, QAbstractItemModel and a MySQL database

    I'm looking for how I should model my model. Basically, the model database contains a bunch of data, which is accessed by a unique name or perhaps ID. The tree view has a set of base items (expandable), which go to a maximum of one level deep.

    Now, to set up the model, is there a way to have the functions query the database directly, or do I need to "cache" the database locally in a QList and look at the data that way? (That's my current solution as I'm somewhat at a loss on how to do it another way).
    Life without passion is death in disguise

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QT4]QTreeView, QAbstractItemModel and a MySQL database

    Quote Originally Posted by KShots
    Now, to set up the model, is there a way to have the functions query the database directly, or do I need to "cache" the database locally in a QList and look at the data that way? (That's my current solution as I'm somewhat at a loss on how to do it another way).
    You can access the database directly but it'll be quite slow, you should have some kind of cache, at least an array (constant size) of most recently or frequently used objects.

  3. #3
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: [QT4]QTreeView, QAbstractItemModel and a MySQL database

    I am having the same problem. TreeModel/View, and the data comes from an SQLite DB or Network. So I need a lazy population of the tree nodes, only reading from the data source when needed.

    Actually, before knowing QT I originally assumed that the view already does some caching and at only asks for data more than once if it is told to do so (model changed or node user interaction on the view). But it even seems to query everything for every repaint of the ui.

    Now I am thinking about a generic model cache that goes between a QT model and view. It would probably cache the ModelIndexes and identify them by their internal id. Not sure if that works out.

    Let's hope that Trolltech will elaborate the already very nice model/view concept. A cache, is needed especially for trees. Otherwise QT users will reinvent buggy tree structures for every QT model, and that is not the idea of QT

    -Alessandro

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QT4]QTreeView, QAbstractItemModel and a MySQL database

    Can't you just use one of QSql*Model? QSqlTableModel for example...

  5. #5
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: [QT4]QTreeView, QAbstractItemModel and a MySQL database

    Quote Originally Posted by wysota
    Can't you just use one of QSql*Model? QSqlTableModel for example...
    I would do that for a List or Table model/view, which boils down to one Sql query. But a tree (in my case) consists of several Sql queries, each one per node. But I will try to use those several QSqlTableModels, and see how the performance is.

    Thanks,
    Alessandro

  6. #6
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: [QT4]QTreeView, QAbstractItemModel and a MySQL database

    A QSqlModel for a tree would be appreciated, though at the moment difficult to visualize. Using a table model for a tree view would not be a good idea, however.

    If I must cache the entire DB before implementing the view, it becomes slightly more difficult and a lot less efficient to use.

    The way I'd envision this to work best is if the tree view were to cache the top-level data, then query everything under a selected leaf (i.e. a '+' is clicked to expand a leaf node) - which, again, would be cached by the view. That way you only query the database when absolutely needed, and you don't do it very often. Scrolling / resizing should have no effect on such a scheme. That's how I had assumed it worked, but I recognize that it's a pretty big assumption.
    Life without passion is death in disguise

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QT4]QTreeView, QAbstractItemModel and a MySQL database

    The view doesn't do any caching by itself. It is always the models responsibility. If you want a "retrieve on demand" approach, you have to code it yourself in the model. For example have a tree which has items and each item can be marked as "complete" or "unknown" and when data from a child of a "unknown" item is fetched, just query the database and attach new elements to the tree before returning the result. You'll have to watch for the "columnCount()" and "rowCount()" methods, because if you don't implement them right, you'll end up with filling the whole tree at the beginning.

  8. #8
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: [QT4]QTreeView, QAbstractItemModel and a MySQL database

    This is the response from the Trolltech QT support. The QAbstractProxyModel seems to be a good possibility to map between different model structures.




    > Is there a way to tell the QTreeView to cache the data and only to
    > requery it when the Model tells it to do so?

    Not out of the box.

    > If I need to create my own cache tree structure (wich I actually fear
    > I need to do), is there something within QT that lets me create such a
    > tree in a comfortable way? Something like the DOM tools but without
    > the XML.

    Not really, but using a combination of QMap<> and QVector<> might be
    what you want, i.e. something like this

    struct Node
    {
    QMap<QString, QVariant> values;
    Node* parent;
    QVector<Node*> children;
    };

    and then provide some accessor functions that add/remove children and
    values or something similar. You could then use the QAbstractProxyModel
    class to provide mapping from your real model (if you have an existing
    one or if you're currently using one of the SQL models).

    Hope this helps.

    --
    Jan Erik Hanssen
    Trolltech AS, Oslo - http://www.trolltech.com

  9. The following user says thank you to Alessandro for this useful post:

    KShots (7th March 2006)

  10. #9
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: [QT4]QTreeView, QAbstractItemModel and a MySQL database

    Thanks. For my case, I'll likely do something different, but that would work for the general case.
    Life without passion is death in disguise

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.