Results 1 to 4 of 4

Thread: QTreeView linked to QSqlTableModel

  1. #1
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    5

    Default QTreeView linked to QSqlTableModel

    I am trying to populate (and later hopefully edit) a QTreeView whose nodes are stored in a SQL table.

    The structure is very simple and similar to a document:

    Qt Code:
    1. Root
    2. |___Text
    3. |___Text
    4. |___Text
    To copy to clipboard, switch view to plain text mode 

    So the table is just:

    ID: Int
    Text: Varchar
    ParentID: Int

    I have read very many posts with similar questions and understand that the key is to use a QAbstractProxyModel between the QTreeView and the QSqlTableModel, in particular to look at implementing mapFromSource and mapToSource. I have also looked at the Interview demo which I think should be a good reference to link a flat structure to a tree.

    Unfortunately being very new to Qt and still learning some of the advanced aspects of C++, I am struggling to fully understand what to do.

    I would therefore really appreciate if someone has got some working code that I can try out and learn from - thanks

  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: QTreeView linked to QSqlTableModel

    I have a working code (somewhere) but unfortunately I can't share it (I don't know where it is anyway). You have to come up with a mapping algorithm from a flat to hierarchical model (and back) and implement it in mapToSource() and mapToSource(). But if you are a beginner, I would suggest to go with a simpler solution - copy data from your flat model to a hierarchical model (like QStandardItemModel) and then after editing it, copy it back to the database.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    5

    Default Re: QTreeView linked to QSqlTableModel

    Thank you for the idea.

    I would still like to pursue the possibility of a direct link between the SQL model and the tree view, however in the meantime if could you offer a short example of how to use the QStandardItemModel that would be very much appreciated!

  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: QTreeView linked to QSqlTableModel

    There are lots of examples of using QStandardItemModel in Qt documentation. Just follow the link in this post.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. not the architecture being linked
    By skizzik in forum Qt Programming
    Replies: 8
    Last Post: 14th October 2010, 09:46
  2. Help: Qt crashed when linked against libSoQt
    By jwieland in forum Qt Programming
    Replies: 18
    Last Post: 14th April 2009, 22:17
  3. How can we use dynamically linked lib in QT?
    By learning_qt in forum Qt Programming
    Replies: 3
    Last Post: 16th December 2008, 13:04
  4. Application seems not to be linked statically
    By Boron in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd April 2008, 17:14
  5. Linked List
    By merry in forum Qt Programming
    Replies: 10
    Last Post: 14th March 2007, 07:07

Tags for this Thread

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.