Results 1 to 4 of 4

Thread: How to create an One-to-Many Relationship in QtSqlRelation ?

  1. #1
    Join Date
    Mar 2010
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to create an One-to-Many Relationship in QtSqlRelation ?

    Hi All,
    I've a problem when I aim to view data into QTableView. I aim to create an One-to-Many (OTM) Relationship in QtSqlRelation. the code can I do is one-to-one like this, I've no Idea for the OTM. How should it be ?.
    Qt Code:
    1. model->setTable("Users");
    2. model->setSort(User_Id, Qt::AscendingOrder);
    3. model->setHeaderData(User_Name,Qt::Horizontal,tr("Username"));
    4. model->setHeaderData(User_FirstName, Qt::Horizontal, tr("First name"));
    5. model->setHeaderData(User_LastName, Qt::Horizontal, tr("Last Name"));
    6. model->setRelation(User_Mode,QSqlRelation("UserModes","ID","Name"));
    7. model->setHeaderData(User_Mode, Qt::Horizontal, tr("Type"));
    8. model->select();
    To copy to clipboard, switch view to plain text mode 

    Thanks

    Hary

  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: How to create an One-to-Many Relationship in QtSqlRelation ?

    The code above doesn't determine if the relation between Users and UserModes is 1-1 or 1-n. You may have several users with the same user mode (1-n) with the code above.
    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. The following user says thank you to wysota for this useful post:

    innobleday (13th March 2010)

  4. #3
    Join Date
    Mar 2010
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create an One-to-Many Relationship in QtSqlRelation ?

    Quote Originally Posted by wysota View Post
    The code above doesn't determine if the relation between Users and UserModes is 1-1 or 1-n. You may have several users with the same user mode (1-n) with the code above.
    Firstly I'm not sure with my code.. Sorry if I miss understanding about 1-n. But the problem is I aim to take several record of several filed in some SQL table called from another Table. In other words, in userMode containf field ID, level1, level2 etc.. and I just want to take level1 level2.. the ID is used to relation between users and usermode. How shoild it be? thaks.

  5. #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: How to create an One-to-Many Relationship in QtSqlRelation ?

    I'm afraid I don't understand what you mean. If you want to join two tables then that's not possible with QSqlRelationalTableModel. You can only substitute data from a column with data from some column from another table. To do more complex things you have to use QSqlQueryModel.
    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. Replies: 2
    Last Post: 28th January 2010, 06:26
  2. How do I create a DLL for QT
    By zartoch in forum Newbie
    Replies: 2
    Last Post: 8th May 2009, 06:28
  3. Replies: 4
    Last Post: 1st May 2009, 11:00
  4. how to create pop-ups
    By miguel_mark in forum Newbie
    Replies: 1
    Last Post: 22nd October 2007, 06:16
  5. What's the relationship between signal/slot and event?
    By twosnowman in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 17:13

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.