Results 1 to 4 of 4

Thread: Questions regarding setting up a Qt SQL Model/View

  1. #1
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Questions regarding setting up a Qt SQL Model/View

    This is not the exact database I am working with, but a simplified version of it showing my main problem. So, let's assume we have the following SQL Database structure:
    Qt Code:
    1. Table A - a list of employees:
    2. id - primary key
    3. name - string describing a persons name
    4.  
    5. Table B - a list of offices:
    6. id - primary key
    7. location - string describing an office location of a person from table a
    8.  
    9. Table C - a list of company cars:
    10. id - primary key
    11. car - string describing the type of car of a person from table a
    12.  
    13. Table A_B_Relation - each person can belong to multiple offices:
    14. a_id - foreign key to table A.id
    15. b_id - foreign key to table B.id
    16.  
    17. Table A_C_Relation - each person can own several company cars:
    18. a_id - foreign key to table A.id
    19. c_id - foreign key to table C.id
    To copy to clipboard, switch view to plain text mode 

    The displayed overview of people and cars they own/offices they belong to should look like the attached image.

    The main problem that bugs me is: How to set up the QSqlRelationalTableModel properly?
    When I set it up using table A as main table then I fail to find a way of displaying the car / office names - QSqlRelation only allows me to display the IDs of each role, or did I miss something in the docs about using it for this kind of thing? Currently I think that I will have to write my own model - am I correct there? It is certainly possible to simply use a QSqlTableModel and set a custom sql query on it which would deliver the data in a way that I could display it as I want using standard Qt classes.
    But then there comes the problem with editing the displayed data in a proper way. I assume that safely editing the fields would require a custom delegate, no?

    I am a bit puzzled if I am trying the correct approach on this issue.
    Attached Images Attached Images
    "If you lie to the compiler, it will get its revenge." - Henry Spencer

  2. #2
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Questions regarding setting up a Qt SQL Model/View

    So, I contemplated a bit more about my problem. Since all my tables are actually models on their own my main problem boils down to combining data from different models in a single view.

    Thus I thought that QAbstractProxyModel derivates should be sufficient for what I want to do, but I do not see an option to map between two different models with it?
    "If you lie to the compiler, it will get its revenge." - Henry Spencer

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Questions regarding setting up a Qt SQL Model/View

    Quote Originally Posted by Methedrine View Post
    Thus I thought that QAbstractProxyModel derivates should be sufficient for what I want to do, but I do not see an option to map between two different models with it?
    You can set only one source model for QAbstractProxyModel, but you can subclass QAbstractItemModel to create multi-source proxy.

    What you want to achieve is not exactly a table, because it displays a hierarchy, so maybe it will be easier if you use QTreeView?

  4. The following user says thank you to jacek for this useful post:

    Methedrine (26th November 2007)

  5. #4
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Questions regarding setting up a Qt SQL Model/View

    Quote Originally Posted by jacek View Post
    You can set only one source model for QAbstractProxyModel, but you can subclass QAbstractItemModel to create multi-source proxy.
    I guess that is what I am looking for, thanks for pointing it out. I was looking so much at models that I must have somehow missed the forest with seeing all those trees

    Quote Originally Posted by jacek View Post
    What you want to achieve is not exactly a table, because it displays a hierarchy, so maybe it will be easier if you use QTreeView?
    Actually it must be a table view, that's one of the requirements. But yes, it certainly resembles some kind of hierarchy. I will keep you people update as I progress on my journey
    "If you lie to the compiler, it will get its revenge." - Henry Spencer

Similar Threads

  1. Model/View -- Sql Insert
    By kroenecker in forum Qt Programming
    Replies: 3
    Last Post: 3rd May 2007, 16:55
  2. Sql questions
    By Nb2Qt in forum Qt Programming
    Replies: 4
    Last Post: 15th February 2007, 23:53

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.