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. Extending QSqlRelationalTableModel

Extending QSqlRelationalTableModel

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 7 Posters 4.8k Views
  • 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.
  • P Panoss

    I 'm trying to extend QSqlRelationalTableModel, in order to try make some changes, e.g return two fields:

    model->setRelation(clientIndex,
               QSqlRelation("clients", "id", "firstname,lastname"));
    

    The above code returns only the first field.
    I want to make it return both fieds, firstname and lastname.

    What I 've done so far (my class is named QSqlRelationalTableModelB):
    qsqlrelationaltablemodelb.h

    #ifndef QSQLRELATIONALTABLEMODELB_H
    #define QSQLRELATIONALTABLEMODELB_H
    
    #include <QtSql/qsqlrelationaltablemodel.h>
    
    class QSqlRelationalTableModelB : public QSqlRelationalTableModel {
      Q_OBJECT
    public:
      explicit QSqlRelationalTableModelB(QString text, QSqlRelationalTableModel *parent = 0);
    };
    
    #endif // QSQLRELATIONALTABLEMODELB_H
    

    qsqlrelationaltablemodelb.cpp

    #include "QSqlRelationalTableModelB.h"
    
    QSqlRelationalTableModelB::QSqlRelationalTableModelB(QString text, QSqlRelationalTableModel *parent) :
      QSqlRelationalTableModel(parent)
    {
        QDebug() << "QSqlRelationalTableModelB!!!"
    }
    

    I create a new instance of the class like this:

    RTMB = new QSqlRelationalTableModelB("hi", this);
    

    But when I build it, I get an error:

      error: C2664: 'QSqlRelationalTableModelB::QSqlRelationalTableModelB(QString,QSqlRelationalTableModel *)' : 
      cannot convert parameter 2 from 'RepairDevices *const ' to 'QSqlRelationalTableModel *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    
    P Offline
    P Offline
    Panoss
    wrote on last edited by
    #11

    I 'm trying this which I describe in my first post:

    @Panoss said in Extending QSqlRelationalTableModel:

    I 'm trying to extend QSqlRelationalTableModel, in order to try make some changes, e.g return two fields:

    model->setRelation(clientIndex,
               QSqlRelation("clients", "id", "firstname,lastname"));
    

    The above code returns only the first field.
    I want to make it return both fieds, firstname and lastname.

    kshegunovK 1 Reply Last reply
    0
    • P Panoss

      I 'm trying this which I describe in my first post:

      @Panoss said in Extending QSqlRelationalTableModel:

      I 'm trying to extend QSqlRelationalTableModel, in order to try make some changes, e.g return two fields:

      model->setRelation(clientIndex,
                 QSqlRelation("clients", "id", "firstname,lastname"));
      

      The above code returns only the first field.
      I want to make it return both fieds, firstname and lastname.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #12

      @Panoss said in Extending QSqlRelationalTableModel:

      I 'm trying this which I describe in my first post

      Yes, I read it, but I didn't understand. Do you mind explaining further? What do you want to return both fields? How would this in your mind work (i.e. how is one relation going to be associated with two tables? And what code returns only the first field, it's just not clear.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Panoss
        wrote on last edited by Panoss
        #13

        I have 2 tables:

        1. devices (fields: id, brand, client_id). The 3nd is foreign key.
        2. clients(with fields: id, firstname, lastname).
          They are connected with devices.client_id = client.id.

        When a device is displayed, I want to be also displayed firstname and lastname from clients.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #14

          Hi,

          What about using a proxy model that would put these two fields together ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Panoss
            wrote on last edited by
            #15

            I don't know what a proxy model is.
            Could you give me an example?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #16

              QSortFilterProxyModel
              QIdentityProxyModel

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Panoss
                wrote on last edited by Panoss
                #17

                I restart this topic (as I had been distracted by other parts of the code):
                I 'm trying to make it with QSortFilterProxyModel (I hope this is the correct way).
                Let me remind you what I'm trying to acomplish:

                I have 2 tables:

                1. devices (fields: id, brand, client_id). The 3nd is foreign key.
                2. clients(with fields: id, firstname, lastname, address).
                They are connected with devices.client_id = client.id.
                

                This is how I make the model ('model' is the model for devices):

                model = new QSqlRelationalTableModel(this);
                model->setTable("devices");
                

                When a device is displayed, I also display lastname from clients in a combo with this code:

                QSqlTableModel *relClientModel = model->relationModel(clientIndex);
                ui.client_id_cbo->setModel(relClientModel);
                ui.client_id_cbo->setModelColumn(relClientModel->fieldIndex("lastname"));  
                

                So, how can I display and firstname and address from clients in two textboxes?

                Like a master/detail form, where master is 'devices' and detail is 'clients', showing one client only (while, usually, in detail we have a list).

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Panoss
                  wrote on last edited by
                  #18

                  Finally, it was proved to be a lot simpler, using the model of the combo:

                  row = ui.client_id_cbo->currentIndex();    
                  QModelIndex idx = ui.client_id_cbo->model()->index(row, 0); // first column
                  QString client_firstname = idx.sibling(idx.row(), 1).data().toString();
                  QString client_address = idx.sibling(idx.row(), 3).data().toString();
                  ui.client_firstname_txt->setPlainText(client_firstname);
                  ui.client_address_txt->setPlainText(client_address);
                  
                  kkmspbK 1 Reply Last reply
                  0
                  • P Panoss

                    Finally, it was proved to be a lot simpler, using the model of the combo:

                    row = ui.client_id_cbo->currentIndex();    
                    QModelIndex idx = ui.client_id_cbo->model()->index(row, 0); // first column
                    QString client_firstname = idx.sibling(idx.row(), 1).data().toString();
                    QString client_address = idx.sibling(idx.row(), 3).data().toString();
                    ui.client_firstname_txt->setPlainText(client_firstname);
                    ui.client_address_txt->setPlainText(client_address);
                    
                    kkmspbK Offline
                    kkmspbK Offline
                    kkmspb
                    wrote on last edited by
                    #19

                    @Panoss Hello! You ask right question.
                    If you need a new functionality of QSqlRelationalTableModel than you should change this class and rebuild qt sources.

                    JonBJ kkmspbK 2 Replies Last reply
                    0
                    • kkmspbK kkmspb

                      @Panoss Hello! You ask right question.
                      If you need a new functionality of QSqlRelationalTableModel than you should change this class and rebuild qt sources.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #20

                      @kkmspb said in Extending QSqlRelationalTableModel:

                      you should change this class and rebuild qt sources

                      ? You should rather subclass QSqlRelationalTableModel and add whatever functionality. You should not change Qt source code for this.

                      1 Reply Last reply
                      1
                      • kkmspbK kkmspb

                        @Panoss Hello! You ask right question.
                        If you need a new functionality of QSqlRelationalTableModel than you should change this class and rebuild qt sources.

                        kkmspbK Offline
                        kkmspbK Offline
                        kkmspb
                        wrote on last edited by
                        #21

                        @kkmspb

                        Maybe you can see our free Qt project (4.8.1, Windows).

                        This project contains several classes inherited from the base classes QSqlTableModel and QTableView.

                        The PblSqlRelationalTableModel class is inherited from QSqlTableModel.

                        The PblTableView class is inherited from QTableView.

                        As a result, a combining class PblTableDlg was created with the composition PblTableView and PblSqlRelationalTableModel.

                        The QTableView class contains full functionality for managing a database table: creating, copying, deleting rows.
                        There is a search, selection by value. Fields in the form of combobox, checkbox,...
                        External links and substitutions from other tabs work automatically.

                        Everything is configured simply in one configuration file config.cpp .

                        All three editing strategies are supported.

                        alt text

                        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