Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlRelationalTableModel - relation with multiple columns
Forum Updated to NodeBB v4.3 + New Features

QSqlRelationalTableModel - relation with multiple columns

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 5.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tupla
    wrote on last edited by
    #1

    Hi everybody.
    I know there is already a post related to this topic ("this one":http://qt-project.org/forums/viewthread/7744), but I don't seem to get this work.
    Problem:
    I have one database table containing a list of people, and another table that has a foreign key to the id of the person. If I wanted to display first name and last name of a person in a table view or while using a mapper, given that first name and last name are in different columns, how could I do this using QSqlRelationalTableModel?

    Link suggested to subclass QAbstractProxyModel, I tried to subclass QSortFilterProxyModel and reimplement the data() function setting the "person" model as source model, but in this way mapper does not work: data are displayed inside the combobox (initially empty), but navigating through the records the value inside the combobox does not change...
    Moreover, I don't understand what should be the source model of my proxy model: should it be the relational model, or the table model "pointed to" by the relation?
    Some hints on how to proceed would be greatly appreciated :)

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Tupla
      wrote on last edited by
      #2

      Does anybody know the solution?

      1 Reply Last reply
      0
      • F Offline
        F Offline
        franku
        wrote on last edited by
        #3

        Keep trying with the QSqlRelationalTableModel. Have you read the "documentation":http://doc.qt.digia.com/qt/qsqlrelationaltablemodel.html? There you can find an example on how to make multiple relations.

        This, Jen, is the internet.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          Tupla
          wrote on last edited by
          #4

          Yes, I read the code snippet related to the creation of multiple relationships, but that's not what I'm trying to achieve.
          I would like to have something like this:

          Table 1: employee
          ID_E | First_name | Last_name | Other

          Table 2: project
          ID_P | Employee

          Setting a QSqlRelationalTableModel on the table "project", and a relationship between the column "Employee" and the ID in the table employee, I would like to have displayed in that column both the first and last names. I see that QSqlRelationalTableModel only allows to set one column for display purpose for each relationship. I would like to display the values of two columns without having to insert an additional column in the database and setting another relationship.
          I hope my problem is clearer in this way.

          Thank you for your reply.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            franku
            wrote on last edited by
            #5
            1. I am no sql expert, but can't you use a SQLView in the database itself for this request (instead of SELECT * from project WHERE...)?

            2. On the other hand you could use just the QSqlTableModel and reimplement the ::data() and ::columnCount() to fake the extra column for the Project's QTableView.

            This, Jen, is the internet.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              Tupla
              wrote on last edited by
              #6

              [quote author="franku" date="1351891016"]1. I am no sql expert, but can't you use a SQLView in the database itself for this request (instead of SELECT * from project WHERE...)?[/quote]
              Do you mean creating a view which concatenates the first and last name and use that as "table" to QSqlTableModel?
              [quote author="franku" date="1351891016"]
              2. On the other hand you could use just the QSqlTableModel and reimplement the ::data() and ::columnCount() to fake the extra column for the Project's QTableView.[/quote]
              That would be an option, I thought about reimplementing those function in QSqlRelationalTableModel to include also the "last_name" column when displaying data for the foreign key reference, but I would prefer a cleaner approach using a proxy model, if there exists one.

              In the link I posted in the first post (which is more or less the same problem I have), there seems to be a solution using a subclass of QAbstractProxyModel, the only thing is that I can't figure it out.

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                QtTester
                wrote on last edited by QtTester
                #7

                I have the same problem, can anyone to solve this?
                for example:
                table1:

                id  shotname longname
                

                table2:

                id list table1Id 
                

                in the QSqlRelationalTableModel example , we can have these fields result:

                id list shotname
                
                // or this
                id list longname
                

                but how can we display more column like this?:

                id list shotname longname
                

                here is my test and work, but CANNOT be editable:

                    QSqlQuery query(db);
                    QSqlQueryModel *m = new QSqlQueryModel(this);
                    query.exec("SELECT table2.id, table2.list, table1.shotname, table1.longname FROM table2"
                               "INNER JOIN table1 ON table2.table1Id= table1.id");
                    m->setQuery(query);
                
                    ui->tableView->setModel(m);
                
                JonBJ 1 Reply Last reply
                0
                • Q QtTester

                  I have the same problem, can anyone to solve this?
                  for example:
                  table1:

                  id  shotname longname
                  

                  table2:

                  id list table1Id 
                  

                  in the QSqlRelationalTableModel example , we can have these fields result:

                  id list shotname
                  
                  // or this
                  id list longname
                  

                  but how can we display more column like this?:

                  id list shotname longname
                  

                  here is my test and work, but CANNOT be editable:

                      QSqlQuery query(db);
                      QSqlQueryModel *m = new QSqlQueryModel(this);
                      query.exec("SELECT table2.id, table2.list, table1.shotname, table1.longname FROM table2"
                                 "INNER JOIN table1 ON table2.table1Id= table1.id");
                      m->setQuery(query);
                  
                      ui->tableView->setModel(m);
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @QtTester
                  The title of this thread was

                  QSqlRelationalTableModel - relation with multiple columns

                  Your solution, and the ones mentioned above, do not relate to a QSqlRelationalTableModel. That can have multiple QSqlRelations if desired. I do not know whether that allows the same column to be mapped twice to two different columns in one foreign table, but you could try it, I can't see why it would not be allowed. Ah, no, I see, it can have multiple setRelation(column, QSqlRelation())s, but each column specified can only have one relation, not multiple ones in a list, it seems.

                  Your SELECT statement is fine, but yes of course what you get from that is a non-editable model.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved