Results 1 to 5 of 5

Thread: getting entries from different sql tables into one model

  1. #1
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default getting entries from different sql tables into one model

    Hello everyone,
    Nice forum you got here, hope you can lend me some of your expertise
    Unfortunately my source code is at work, where I don´t have a direct net connection, so I´ll try to put my problem into a few words.
    I got a few SQL tables:

    table address
    ID City street nameRef
    04 NY 5thAv 05
    06 LA HBvd 06
    09 SA whtever 08

    table name
    ID first sur
    05 A. Peters
    06 B. Jennings
    08 C. Schlegel


    What I want to have in the end is a table view showing me all this data in one table, e.g:
    ID City street first sur
    04 NY 5thAv A. Peters
    06 LA HBvd B. Jennings
    09 SA whtever C. Schlegel
    (Bad formatting, but you should get the idea)

    It´s easy to get one field from "table name" into "table address" by using QSqlRelationalTableModel and defining a relation. The problem now of course is, that I got 2 (and more in my real application) fields to be inserted instead of one. My first thought was to insert the number of needed columns, copy the "nameref" numbers into it and define as many relations as needed. This doesn´t work though, especially as I´m not allowed to edit the original database.
    I hope I´m wrong on this one, looking forward to all replies.
    Last edited by SK; 8th March 2006 at 20:26.

  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: getting entries from different sql tables into one model

    Use QSqlQueryModel. Just remember it is a read-only model. If you want to make it writable, you have to subclass and implement that functionality yourself.

  3. The following user says thank you to wysota for this useful post:

    SK (9th March 2006)

  4. #3
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: getting entries from different sql tables into one model

    Thanks, that actually worked fine. The only minor issue I´m having now is that the table view´s sortbyColumn function (activated via click on the respective header) doesn´t work anymore. Is there any function I have to implement in QSqlQueryModel that will give back this functionality? It worked fine with the QSqlTableModel I used before.

  5. #4
    Join Date
    Feb 2006
    Location
    Boulder, Colorado, USA
    Posts
    63
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: getting entries from different sql tables into one model

    try to link the click on the header to a custom slot which creates an "order by" part to your query and then runs it again. You will need to maintain a map of columns to column names though, to implement it.

  6. #5
    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: getting entries from different sql tables into one model

    Quote Originally Posted by SK
    Thanks, that actually worked fine. The only minor issue I´m having now is that the table view´s sortbyColumn function (activated via click on the respective header) doesn´t work anymore. Is there any function I have to implement in QSqlQueryModel that will give back this functionality? It worked fine with the QSqlTableModel I used before.
    Use a proxy model (QSortFilterProxyModel or something like that).

Similar Threads

  1. Replies: 6
    Last Post: 22nd November 2011, 03: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.