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 Offline
    P Offline
    Panoss
    wrote on 7 Feb 2017, 11:32 last edited by Panoss 2 Jul 2017, 11:56
    #1

    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 1 Reply Last reply 7 Feb 2017, 17:22
    0
    • V Offline
      V Offline
      VRonin
      wrote on 7 Feb 2017, 11:36 last edited by
      #2

      change explicit QSqlRelationalTableModelB(QString text, QSqlRelationalTableModel *parent = 0);
      to explicit QSqlRelationalTableModelB(QString text, QObject*parent = 0);

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • P Offline
        P Offline
        Panoss
        wrote on 7 Feb 2017, 11:54 last edited by
        #3

        VRonin, I made the change you suggested, but now I get another error:

        repairdevices.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall QSqlRelationalTableModelB::QSqlRelationalTableModelB(class QString,class QObject *)" (??0QSqlRelationalTableModelB@@QAE@VQString@@PAVQObject@@@Z) referenced in function "public: __thiscall RepairDevices::RepairDevices(void)" (??0RepairDevices@@QAE@XZ)
        
        J 1 Reply Last reply 7 Feb 2017, 11:56
        0
        • P Panoss
          7 Feb 2017, 11:54

          VRonin, I made the change you suggested, but now I get another error:

          repairdevices.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall QSqlRelationalTableModelB::QSqlRelationalTableModelB(class QString,class QObject *)" (??0QSqlRelationalTableModelB@@QAE@VQString@@PAVQObject@@@Z) referenced in function "public: __thiscall RepairDevices::RepairDevices(void)" (??0RepairDevices@@QAE@XZ)
          
          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 7 Feb 2017, 11:56 last edited by
          #4

          @Panoss Did you apply this change to the method definition in cpp file as well?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • P Offline
            P Offline
            Panoss
            wrote on 7 Feb 2017, 12:01 last edited by Panoss 2 Jul 2017, 12:17
            #5

            jsulm you 're right, I hadn't.
            Idid, and now my files are:

            qsqlrelationaltablemodelb.h:

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

            qsqlrelationaltablemodelb.cpp:

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

            But now I get the error:

            repairdevices.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall QSqlRelationalTableModelB::QSqlRelationalTableModelB(class QString,class QObject *)" (??0QSqlRelationalTableModelB@@QAE@VQString@@PAVQObject@@@Z) referenced in function "public: __thiscall RepairDevices::RepairDevices(void)" (??0RepairDevices@@QAE@XZ)
            
            1 Reply Last reply
            0
            • P Offline
              P Offline
              Panoss
              wrote on 7 Feb 2017, 12:16 last edited by
              #6

              I deleted all files from the debug folder, rebuilt it and run fine!
              I suppose it was some file in the debug folder.

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Panoss
                wrote on 7 Feb 2017, 12:20 last edited by Panoss 2 Jul 2017, 12:21
                #7

                I took a look in the file qsqlrelationaltablemodel.
                As you see, it 's the 5.0.2 version because I use Qt 5.0.2.
                As you also see, it has 749 lines of code...
                I 'm a little bit desperate...:)

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Panoss
                  wrote on 7 Feb 2017, 12:51 last edited by Panoss 2 Jul 2017, 12:53
                  #8

                  I 'm trying to override function setRelations.

                  .h:

                  #ifndef QSQLRELATIONALTABLEMODELB_H
                  #define QSQLRELATIONALTABLEMODELB_H
                  
                  #include <QtSql/qsqlrelationaltablemodel.h>
                  
                  class QSqlRelationalTableModelB : public QSqlRelationalTableModel {
                      Q_OBJECT
                  public:
                      explicit QSqlRelationalTableModelB(QString text, QObject *parent = 0);
                      virtual void setRelation(int column, const QSqlRelation &relation);
                  };
                  
                  #endif // QSQLRELATIONALTABLEMODELB_H
                  

                  .cpp:

                  #include "QSqlRelationalTableModelB.h"
                  #include "QDebug"
                  
                  QT_BEGIN_NAMESPACE
                  
                  QSqlRelationalTableModelB::QSqlRelationalTableModelB(QString text, QObject *parent) :
                    QSqlRelationalTableModel(parent)
                  {
                      qDebug() << "QSqlRelationalTableModelB!!!";
                  }
                  
                  void QSqlRelationalTableModelB::setRelation(int column, const QSqlRelation &relation)
                  {
                      Q_D(QSqlRelationalTableModelB);
                      if (column < 0)
                          return;
                      if (d->relations.size() <= column)
                          d->relations.resize(column + 1);
                      d->relations[column].init(this, relation);
                  }
                  
                  
                  QT_END_NAMESPACE
                  

                  I get quite a few errors:

                  ...\qsqlrelationaltablemodelb.cpp:14: error: C2065: 'QSqlRelationalTableModelBPrivate' : undeclared identifier
                  
                  ...\qsqlrelationaltablemodelb.cpp:14: error: C2059: syntax error : 'const'
                  
                  ...\qsqlrelationaltablemodelb.cpp:17: error: C2065: 'd' : undeclared identifier
                  

                  And a few more...

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    Panoss
                    wrote on 7 Feb 2017, 14:57 last edited by
                    #9

                    I think it has to do with Q_D(QSqlRelationalTableModelB).
                    I read some things here, but I can't say I understand much.

                    kshegunovK 1 Reply Last reply 7 Feb 2017, 16:09
                    0
                    • P Panoss
                      7 Feb 2017, 14:57

                      I think it has to do with Q_D(QSqlRelationalTableModelB).
                      I read some things here, but I can't say I understand much.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on 7 Feb 2017, 16:09 last edited by kshegunov 2 Jul 2017, 16:14
                      #10

                      Q_D is the d-pointer expansion macro. Long story short, to use that with the private object of the relational table model Qt has, you need to create one additional class that inherits from QSqlRelationalTableModelPrivate (QT += sql-private) and implement a constructor that passes an instance from the inherited private class to the parent class, i.e. QSqlRelationTableModel. Now, that's hacking into the Qt's internals (which is not documented and breaks binary compatibility) and I'm not completely convinced you really need it. What exactly are you striving to accomplish?

                      I think you actually want to use QSqlRelationalDelegate instead.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      2
                      • P Panoss
                        7 Feb 2017, 11:32

                        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 7 Feb 2017, 17:22 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 7 Feb 2017, 17:49
                        0
                        • P Panoss
                          7 Feb 2017, 17:22

                          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 7 Feb 2017, 17:49 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 7 Feb 2017, 17:58 last edited by Panoss 2 Jul 2017, 18:04
                            #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 7 Feb 2017, 22:51 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 8 Feb 2017, 05:10 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 8 Feb 2017, 12:44 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 16 Feb 2017, 17:31 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 16 Feb 2017, 19:11 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 5 Jun 2022, 09:15
                                      0
                                      • P Panoss
                                        16 Feb 2017, 19:11

                                        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 5 Jun 2022, 09:15 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 5 Jun 2022, 09:59
                                        0
                                        • kkmspbK kkmspb
                                          5 Jun 2022, 09:15

                                          @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 5 Jun 2022, 09:59 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

                                          • Login

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