Results 1 to 4 of 4

Thread: how to use QTreeView with Database model

  1. #1
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default how to use QTreeView with Database model

    sorry if this is suppossed to be obvious, but i only see examples on how to use the QTreeView with QDir. I would like to use the result of a DB-query. Could anyone please point me towards information what my data has to look like? i mean: do i need a column called parent and one called id or how does it work that the Tree knows how to use the data in the model?

  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: how to use QTreeView with Database model

    Relation databases gather data in tabular form. There is no sense to use a tree with such data. You have to provide some parent-child structure, but you have to do that yourself, the database won't do that for you.

    BTW. What do you mean by "use QTreeview with QDir" and that you want to use a database query with it?

  3. #3
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to use QTreeView with Database model

    i understand that i will have to structure my data in a way that it makes sense in a tree.
    The data came in the form of
    01|NULL|NULL|Text
    01|01|NULL|Text
    01|01|01|Text
    01|01|02|Text

    so it makes sense to show it in a Tree but i doubt that if i send a tablemodel with this data to the QTreeView Object it will be able to work with that.
    Do i need to build a nested set or do i need to have a parent id in some column or how else do i need to prepare my data to give it to the tree?


    btw: what i meant with the QDir was the QDirModel which is always used to explain the QTreeView, as in http://doc.trolltech.com/4.0/qtreeview.html
    and no i don't want to use this i hope to be able to use a QSqlQueryModel or something like that.

  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: how to use QTreeView with Database model

    You should be able to do this quickly using a two layered model. Lower layer should be QSqlTableModel or QSqlQueryModel and on top of that you'd have to implement a proxy model which will map indexes from the sql model to a tree structure (see docs for the proxy model for details).

Similar Threads

  1. Replies: 4
    Last Post: 6th February 2009, 19:18
  2. QTreeView and QAbstractItem Model.
    By Terabyte in forum Qt Programming
    Replies: 1
    Last Post: 13th January 2009, 13:18
  3. QTreeView restore Expanded node after reload model
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2008, 17:42
  4. using QTreeView with a Database model
    By darksaga in forum Qt Programming
    Replies: 1
    Last Post: 17th January 2007, 23:29
  5. Modify model data in QTreeView
    By YuriyRusinov in forum Qt Programming
    Replies: 6
    Last Post: 26th October 2006, 17:28

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.