Results 1 to 2 of 2

Thread: Show QSqlQueryModel in QTableView without sort or union

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Show QSqlQueryModel in QTableView without sort or union

    Let's say there are two tables,

    Table 1:
    course_ID
    1
    2
    ...

    Table 2:
    class_ID........student_ID............course_ID
    ......1.................1......................... .2
    ......1.................2......................... .1
    ......1.................3......................... .2
    ......1.................4......................... .1

    My code as follow:
    Qt Code:
    1. courseModel=new QSqlQueryModel();
    2. courseModel->setQuery("SELECT *FROM Table1 WHERE course_ID IN (SELECT course_ID FROM Table2 WHERE class_ID=1)");
    3. ui->courseModelView->setmodel(courseModel);
    4. ui->courseModelView->show();
    To copy to clipboard, switch view to plain text mode 

    result is :

    course_ID
    1
    2

    what i want is without sorting and union:

    course_ID
    2
    1
    2
    1

    second question is that since i use sqlite3 as database, table is column head (horizontal), but i want show it vertical as:

    course_ID 2 1 2 1

    in QTableView

    Any solution or ideas? Thank you in advance!

  2. #2
    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: Show QSqlQueryModel in QTableView without sort or union

    There is no sorting or union involved in your query. You are asking for all the rows in table 1 where the course_id is in table 2 with classID = 1. There are only two rows in table 1 and therefore at most two rows in the output.

    Assuming there are actually other columns in table 1 then what you want is a join.
    Qt Code:
    1. SELECT table1.*
    2. FROM table1 INNER JOIN table2
    3. ON table1.courseID = table2.courseID
    4. WHERE table2.classID = 1
    To copy to clipboard, switch view to plain text mode 
    If there are no other columns in table 1 then you don't need that table at all.
    Unless you explicitly apply some sort order to the data there are no guarantees about the order of the output, though usually you get them in the order they were inserted.

    If you wish to transpose the rows and columns from the model then you should either:
    • Use a proxy model to transpose the data. There's an example [wiki]Transpose Proxy Model[/wiki] in the wiki.
    • Use a more complicated query to extract the data that way, but this comes with lots of limitations.

Similar Threads

  1. Multiples QSqlQueryModel and QTableView
    By Netheril in forum Qt Programming
    Replies: 3
    Last Post: 7th April 2010, 18:38
  2. Replies: 2
    Last Post: 2nd March 2010, 04:24
  3. QTableView/QSqlQueryModel
    By norobro in forum Qt Programming
    Replies: 7
    Last Post: 15th February 2008, 21:52
  4. QTableView, QSqlQueryModel and delegates
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2008, 12:04
  5. QSqlQueryModel and QTableView question
    By wbt_ph in forum Newbie
    Replies: 4
    Last Post: 20th September 2006, 15:40

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
  •  
Qt is a trademark of The Qt Company.