Results 1 to 11 of 11

Thread: Joining tables into QTableView

  1. #1
    Join Date
    May 2010
    Location
    Rzeszow/Poland
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Joining tables into QTableView

    Hi!

    I have the following tables structure:

    Qt Code:
    1. classes (
    2. id integer primary key,
    3. name integer
    4. );
    5.  
    6. subjects (
    7. id integer primary key,
    8. name varchar,
    9. short_name varchar
    10. );
    11.  
    12. subjects_relations (
    13. classID integer foreign key,
    14. subjectID integer foreign key
    15. );
    16.  
    17. students (
    18. id integer primary key,
    19. name varchar,
    20. surname varchar,
    21. class integer foreign key
    22. );
    23.  
    24. marks (
    25. id integer primary key,
    26. value integer, //in Poland we have digit marks ;)
    27. studentID integer foreign key,
    28. subjectID integer foreign key
    29. );
    To copy to clipboard, switch view to plain text mode 

    Ps.: Only one mark can be connected with specified subject and student.

    Now: I want to make a table (QTableView) like this:

    Qt Code:
    1. ***************| math | eng
    2. -----------------------------
    3. David Smiths | 3 | 5
    4. John Doe | 5 | 4
    To copy to clipboard, switch view to plain text mode 
    So: columns are subjects connected to a specified class. Rows are students from this class. Cells are marks. Columns labels are subject short_name (this is no problem) and rows labels are name and surname (this is a problem).

    How to make that table with one of the Sql Models? I want to mention that I want to edit marks in table and save them with submitAll();.

    Cheers,
    David

  2. #2
    Join Date
    May 2010
    Location
    Rzeszow/Poland
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Joining tables into QTableView

    Anyone? Please...

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Joining tables into QTableView

    I would start with a QSqlRelationalTableModel.

    Set it to use the marks table and then use setRelation to fill in the foreign keys.

    See for an example here: http://doc.qt.nokia.com/4.6/sql-rela...model-cpp.html

  4. #4
    Join Date
    May 2010
    Location
    Rzeszow/Poland
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Joining tables into QTableView

    What about setting vertical labels to Sql Model?

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Joining tables into QTableView

    By default, only the labels for the horizontal header are set.
    To set custom labels for your vertical header, subclass the model and reimplement headerData(...)

  6. #6
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Joining tables into QTableView

    There is also another way - that is to create a view of the joined tables then set the model table to the view.

  7. #7
    Join Date
    May 2010
    Location
    Rzeszow/Poland
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Joining tables into QTableView

    Thanks tbscope.

    Quote Originally Posted by waynew View Post
    There is also another way - that is to create a view of the joined tables then set the model table to the view.
    Unfortunately views in SQLite are read-only.

  8. #8
    Join Date
    May 2010
    Location
    Rzeszow/Poland
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Joining tables into QTableView

    I don't belive it's so hard...

    Ok, forget about vertical labels. I have 3 questions.

    1. Is this even possible?

    2. Let's say I have query that selects data in way I want. Is there possibility to edit QSqlQueryModel like QSqlTableModel? I know that I have to subclass QSqlQueryModel, but I can't.

    3. If above ways are not possible, how should I manage data (maybe files, or XML?) to make my program.

  9. #9
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Joining tables into QTableView

    Do you have a SQL query that displays the data as you want? It looks like you want vector data in both the horizontal and vertical directions. Short of a lot of joins, I'm interested in how this could be achieved. If you have an SQL statement, it should be possible to create a manual model.

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Joining tables into QTableView

    The table is a classic pivot table (or cross-tab). How this is constructed in SQL varies from vendor to vendor. Difficulty varies and sometimes can only be done with a temporary table.

    Assuming you have a query that will generate the pivoted table: Updating the underlying data through a model displaying the pivot table through QSqlQueryModel is not possible. You will have to subclass QSqlQueryModel to provide the setData() method and the flags() method. There's an example in the reference documentation: Creating New Models

    Another approach could be creating a derivative of QAbstractTableModel (or QSqlTableModel) and overriding the data(), setData() and flags() virtual methods. Internally the data() method could take the row and column, map them to a student and subject and perform a query to find the right value to return. On setData() the query generated is an UPDATE against the correct table(s).
    Last edited by ChrisW67; 7th June 2010 at 00:06.

  11. The following user says thank you to ChrisW67 for this useful post:

    numbat (7th June 2010)

  12. #11
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Joining tables into QTableView

    If you have SQL statements to read and delete, you can probably use the manual SQL query model I posted here.

Similar Threads

  1. Replies: 1
    Last Post: 16th May 2010, 19:25
  2. Way to load all of data from 2 tables into one QTableView?
    By Kevin Hoang in forum Qt Programming
    Replies: 8
    Last Post: 3rd April 2010, 09:42
  3. tables
    By tommy in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 15:17
  4. nested tables
    By Jeroen van der Waal in forum Qt Programming
    Replies: 1
    Last Post: 6th June 2007, 17:12

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.