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 with multiple Joins?
Forum Updated to NodeBB v4.3 + New Features

QSqlRelationalTableModel with multiple Joins?

Scheduled Pinned Locked Moved Unsolved General and Desktop
67 Posts 4 Posters 22.9k Views 1 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #44

    One way could be to keep a vector of edited cells that you update when setData is called with the EditRole and that you will use when data is called for the BackgroundRole and you return the colour you want.

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

    devhobbyD 1 Reply Last reply
    1
    • SGaistS SGaist

      One way could be to keep a vector of edited cells that you update when setData is called with the EditRole and that you will use when data is called for the BackgroundRole and you return the colour you want.

      devhobbyD Offline
      devhobbyD Offline
      devhobby
      wrote on last edited by
      #45

      @SGaist said in QSqlRelationalTableModel with multiple Joins?:

      One way could be to keep a vector of edited cells that you update when setData is called with the EditRole and that you will use when data is called for the BackgroundRole and you return the colour you want.

      Thank you. The problem is I can't find the method which allows me to change te color to a cell. I guess something like setBackgroundColor(row,column)

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

        Because there's none. Do you have a custom setData method ? If so you should emit the dataChanged signal properly and it should trigger an update of the view which should request all the roles including the one for background colour.

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

        devhobbyD 1 Reply Last reply
        0
        • SGaistS SGaist

          Because there's none. Do you have a custom setData method ? If so you should emit the dataChanged signal properly and it should trigger an update of the view which should request all the roles including the one for background colour.

          devhobbyD Offline
          devhobbyD Offline
          devhobby
          wrote on last edited by
          #47

          @SGaist said in QSqlRelationalTableModel with multiple Joins?:

          which should request all the roles including the one for background colour.

          That! I don't know how to make such a request for the table view.

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

            That's what dataChanged is for.

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

            devhobbyD 1 Reply Last reply
            2
            • SGaistS SGaist

              That's what dataChanged is for.

              devhobbyD Offline
              devhobbyD Offline
              devhobby
              wrote on last edited by
              #49

              @SGaist said in QSqlRelationalTableModel with multiple Joins?:

              That's what dataChanged is for.

              In fact I emit the signal including the role

              emit dataChanged(index, index, roleArray);
              

              roleArray includes Qt::BackgroundColor

              Now what? Where do I select the color the cell has to become?

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

                Re-implement the data method and handle the BackgroundRole special case there.

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

                devhobbyD 1 Reply Last reply
                1
                • SGaistS SGaist

                  Re-implement the data method and handle the BackgroundRole special case there.

                  devhobbyD Offline
                  devhobbyD Offline
                  devhobby
                  wrote on last edited by
                  #51

                  @SGaist said in QSqlRelationalTableModel with multiple Joins?:

                  Re-implement the data method and handle the BackgroundRole special case there.

                  I now return the red color when the role is Qt::BackgroundColor

                  if (!item.isValid())
                     return QVariant();
                  
                  if(role == Qt::BackgroundRole)
                      return QColor(255, 0, 0);
                  
                  return QVariant();
                  

                  My Table View now is all red without changing anything!

                  Also, is it good to return a default-constructed QVariant when no particular criteria are met?

                  JonBJ 1 Reply Last reply
                  0
                  • devhobbyD devhobby

                    @SGaist said in QSqlRelationalTableModel with multiple Joins?:

                    Re-implement the data method and handle the BackgroundRole special case there.

                    I now return the red color when the role is Qt::BackgroundColor

                    if (!item.isValid())
                       return QVariant();
                    
                    if(role == Qt::BackgroundRole)
                        return QColor(255, 0, 0);
                    
                    return QVariant();
                    

                    My Table View now is all red without changing anything!

                    Also, is it good to return a default-constructed QVariant when no particular criteria are met?

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

                    @devhobby

                    My Table View now is all red without changing anything!

                    Have you understood you now need to look up the cell coordinates (item) in the vector of changed cells, as per:

                    @SGaist said in QSqlRelationalTableModel with multiple Joins?:

                    One way could be to keep a vector of edited cells that you update when setData is called with the EditRole and that you will use when data is called for the BackgroundRole and you return the colour you want.

                    devhobbyD 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @devhobby

                      My Table View now is all red without changing anything!

                      Have you understood you now need to look up the cell coordinates (item) in the vector of changed cells, as per:

                      @SGaist said in QSqlRelationalTableModel with multiple Joins?:

                      One way could be to keep a vector of edited cells that you update when setData is called with the EditRole and that you will use when data is called for the BackgroundRole and you return the colour you want.

                      devhobbyD Offline
                      devhobbyD Offline
                      devhobby
                      wrote on last edited by
                      #53

                      @JonB @SGaist said in QSqlRelationalTableModel with multiple Joins?:

                      @devhobby

                      My Table View now is all red without changing anything!

                      Have you understood you now need to look up the cell coordinates (item) in the vector of changed cells, as per:

                      @SGaist said in QSqlRelationalTableModel with multiple Joins?:

                      One way could be to keep a vector of edited cells that you update when setData is called with the EditRole and that you will use when data is called for the BackgroundRole and you return the colour you want.

                      I did it.

                      But now all the text is gone!

                      0_1518131506845_a38413b2-1d30-4f3d-95d3-629b057ec602-image.png

                      The color is applied to the modified cells.

                      But there's no text! Even if I don't edit any cell, the text is gone!

                      JonBJ 1 Reply Last reply
                      0
                      • devhobbyD devhobby

                        @JonB @SGaist said in QSqlRelationalTableModel with multiple Joins?:

                        @devhobby

                        My Table View now is all red without changing anything!

                        Have you understood you now need to look up the cell coordinates (item) in the vector of changed cells, as per:

                        @SGaist said in QSqlRelationalTableModel with multiple Joins?:

                        One way could be to keep a vector of edited cells that you update when setData is called with the EditRole and that you will use when data is called for the BackgroundRole and you return the colour you want.

                        I did it.

                        But now all the text is gone!

                        0_1518131506845_a38413b2-1d30-4f3d-95d3-629b057ec602-image.png

                        The color is applied to the modified cells.

                        But there's no text! Even if I don't edit any cell, the text is gone!

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

                        @devhobby
                        What? You want text as well as color? ;-)

                        I think you'll need to show us your data() function now?

                        devhobbyD 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @devhobby
                          What? You want text as well as color? ;-)

                          I think you'll need to show us your data() function now?

                          devhobbyD Offline
                          devhobbyD Offline
                          devhobby
                          wrote on last edited by devhobby
                          #55

                          @JonB said in QSqlRelationalTableModel with multiple Joins?:

                          @devhobby
                          What? You want text as well as color? ;-)

                          I think you'll need to show us your data() function now?

                          Oh God, don't tell me it's another pain in the neck!

                          QVariant MyModel::data(const QModelIndex& item, int role) const
                          {
                              if(role == Qt::BackgroundRole)
                              {
                                  if(MainWindow::cellsEdited.contains(item))
                                      return QColor(66, 197, 244, 150);
                              }
                          
                              return QVariant();
                          }
                          

                          Yes... I want the background color to stay behind the text, of course...

                          JonBJ 1 Reply Last reply
                          0
                          • devhobbyD devhobby

                            @JonB said in QSqlRelationalTableModel with multiple Joins?:

                            @devhobby
                            What? You want text as well as color? ;-)

                            I think you'll need to show us your data() function now?

                            Oh God, don't tell me it's another pain in the neck!

                            QVariant MyModel::data(const QModelIndex& item, int role) const
                            {
                                if(role == Qt::BackgroundRole)
                                {
                                    if(MainWindow::cellsEdited.contains(item))
                                        return QColor(66, 197, 244, 150);
                                }
                            
                                return QVariant();
                            }
                            

                            Yes... I want the background color to stay behind the text, of course...

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

                            @devhobby
                            You're supposed to be only handling Qt::BackgroundRole special case. The rest of the time presumably you want to return the inherited class's implementation of data(). So: instead of your return QVariant(); catch-all, you want whatever it is (remember I'm not C++) for return base::data(item, role);.

                            The idea is: your overload is not called only for the color (which is when role == Qt::BackgroundRole), it's called loads of other times for quite different information (including the text) with other values of role. You were returning an empty QVariant for any other "property" of the cell, including its text! That's how it works.

                            BTW, if it's any consolation, I'm as new to this as you are. So I didn't know it worked this way till earlier too.

                            devhobbyD 1 Reply Last reply
                            2
                            • JonBJ JonB

                              @devhobby
                              You're supposed to be only handling Qt::BackgroundRole special case. The rest of the time presumably you want to return the inherited class's implementation of data(). So: instead of your return QVariant(); catch-all, you want whatever it is (remember I'm not C++) for return base::data(item, role);.

                              The idea is: your overload is not called only for the color (which is when role == Qt::BackgroundRole), it's called loads of other times for quite different information (including the text) with other values of role. You were returning an empty QVariant for any other "property" of the cell, including its text! That's how it works.

                              BTW, if it's any consolation, I'm as new to this as you are. So I didn't know it worked this way till earlier too.

                              devhobbyD Offline
                              devhobbyD Offline
                              devhobby
                              wrote on last edited by devhobby
                              #57

                              @JonB said in QSqlRelationalTableModel with multiple Joins?:

                              @devhobby
                              You're supposed to be only handling Qt::BackgroundRole special case. The rest of the time presumably you want to return the inherited class's implementation of data(). So: instead of your return QVariant(); catch-all, you want whatever it is (remember I'm not C++) for return base::data(item, role);.
                              The idea is: your overload is not called only for the color (which is when role == Qt::BackgroundRole), it's called loads of other times for quite different information (including the text) with other values of role. You were returning an empty QVariant for any other "property" of the cell, including its text! That's how it works.
                              BTW, if it's any consolation, I'm as new to this as you are. So I didn't know it worked this way till earlier too.

                              Oh ok thanks, it worked!

                              Do you know, by chance, in which order are setData() and data() called? When is data() specifically called? When a dataChanged signal is emitted?

                              JonBJ 1 Reply Last reply
                              0
                              • devhobbyD devhobby

                                @JonB said in QSqlRelationalTableModel with multiple Joins?:

                                @devhobby
                                You're supposed to be only handling Qt::BackgroundRole special case. The rest of the time presumably you want to return the inherited class's implementation of data(). So: instead of your return QVariant(); catch-all, you want whatever it is (remember I'm not C++) for return base::data(item, role);.
                                The idea is: your overload is not called only for the color (which is when role == Qt::BackgroundRole), it's called loads of other times for quite different information (including the text) with other values of role. You were returning an empty QVariant for any other "property" of the cell, including its text! That's how it works.
                                BTW, if it's any consolation, I'm as new to this as you are. So I didn't know it worked this way till earlier too.

                                Oh ok thanks, it worked!

                                Do you know, by chance, in which order are setData() and data() called? When is data() specifically called? When a dataChanged signal is emitted?

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

                                @devhobby
                                Well, I presume:

                                • setData() is called whenever the data is changed/set
                                • dataChanged signal should be emitted by setData() whenever new data is different from current data
                                • data() is called many times, with whatever role aspect is wanted, not only by your code but also by Qt code whenever it wants a piece of information

                                http://doc.qt.io/qt-5/qt.html#ItemDataRole-enum:

                                enum Qt::ItemDataRole

                                Each item in the model has a set of data elements associated with it, each with its own role. The roles are used by the view to indicate to the model which type of data it needs. Custom models should return data in these types.

                                devhobbyD 1 Reply Last reply
                                1
                                • JonBJ JonB

                                  @devhobby
                                  Well, I presume:

                                  • setData() is called whenever the data is changed/set
                                  • dataChanged signal should be emitted by setData() whenever new data is different from current data
                                  • data() is called many times, with whatever role aspect is wanted, not only by your code but also by Qt code whenever it wants a piece of information

                                  http://doc.qt.io/qt-5/qt.html#ItemDataRole-enum:

                                  enum Qt::ItemDataRole

                                  Each item in the model has a set of data elements associated with it, each with its own role. The roles are used by the view to indicate to the model which type of data it needs. Custom models should return data in these types.

                                  devhobbyD Offline
                                  devhobbyD Offline
                                  devhobby
                                  wrote on last edited by
                                  #59

                                  @JonB said in QSqlRelationalTableModel with multiple Joins?:

                                  @devhobby
                                  Well, I presume:

                                  • setData() is called whenever the data is changed/set
                                  • dataChanged signal should be emitted by setData() whenever new data is different from current data
                                  • data() is called many times, with whatever role aspect is wanted, not only by your code but also by Qt code whenever it wants a piece of information

                                  http://doc.qt.io/qt-5/qt.html#ItemDataRole-enum:

                                  enum Qt::ItemDataRole

                                  Each item in the model has a set of data elements associated with it, each with its own role. The roles are used by the view to indicate to the model which type of data it needs. Custom models should return data in these types.

                                  What I have noticed:

                                  1. Double-Click signal on the Table View calls slot function onTableChanged()
                                  2. onTableChanged() calls data() when it has finished
                                    <>
                                  3. Flags are checked
                                  4. setData() is called if flag is editable
                                  5. Signal dataChanged is emitted by setData()

                                  Where's the problem?

                                  Say I want to allow modification of some cells (name, surname...) and forbid modification of others (primary key, foreign key...)

                                  I have a vector of immutable columns (0, 3, 5...)

                                  I have to check if the column being edited belongs to the vector of immutable columns... TWICE!

                                  One inside the flag() function and one inside onTableChanged()

                                  • Doing the check in flags() keeps me from modifying the content of the cell (hence, disabling the double-click)
                                  • Doing the check in onTableChanged() keeps the cells from being added to the vector of cells that need to be coloured by data()

                                  Both checks are identical, but serve for 2 different purposes: one for disabling the double-click, the other for disabling the coloration.

                                  All of these events are not sequential. I put a symbol <> in the list above to evidence two apparently unrelated events.

                                  If these events had been sequential, I would've written only ONE check at the beginning of the event.

                                  So, the question is: should I keep the situation this way (2 checks)?

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

                                    If you overwrote the flags method correctly, you shouldn't be able to edit the corresponding cell so you shouldn't have to make several checks.

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

                                    devhobbyD 1 Reply Last reply
                                    1
                                    • devhobbyD devhobby

                                      @VRonin said in QSqlRelationalTableModel with multiple Joins?:

                                      See https://forum.qt.io/topic/85973/how-to-simply-change-the-background-colour-of-a-cell-inside-a-tableview

                                      Thanks but before doing that, I was wondering if there's a way to change the content of the cell by directly typing into it.

                                      I just want to change the data of the cell and, once changed, color that cell in a different color to alert the user of the change of that particular item/cell.

                                      Seems a lot of hard-coding work... maybe I should find another convenient way... but for now let's see what comes out

                                      VRoninV Offline
                                      VRoninV Offline
                                      VRonin
                                      wrote on last edited by
                                      #61

                                      @devhobby said in QSqlRelationalTableModel with multiple Joins?:

                                      @VRonin said in QSqlRelationalTableModel with multiple Joins?:

                                      See https://forum.qt.io/topic/85973/how-to-simply-change-the-background-colour-of-a-cell-inside-a-tableview

                                      Thanks but before doing that

                                      This discussion went on way too long because you didn't bother looking at that link.

                                      The solution is already presented there. Basically you need a proxy between the QSqlQueryModel and the view that can handle data changes. That link presents you with an example implementation. You can then use the dataChanged signal of the proxy to detect things you probably want to send to db

                                      P.S.
                                      Be careful that proxy considers Qt::EditRole as separate from Qt::DisplayRole

                                      "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
                                      0
                                      • devhobbyD devhobby

                                        @JonB said in QSqlRelationalTableModel with multiple Joins?:

                                        @devhobby
                                        Well, I presume:

                                        • setData() is called whenever the data is changed/set
                                        • dataChanged signal should be emitted by setData() whenever new data is different from current data
                                        • data() is called many times, with whatever role aspect is wanted, not only by your code but also by Qt code whenever it wants a piece of information

                                        http://doc.qt.io/qt-5/qt.html#ItemDataRole-enum:

                                        enum Qt::ItemDataRole

                                        Each item in the model has a set of data elements associated with it, each with its own role. The roles are used by the view to indicate to the model which type of data it needs. Custom models should return data in these types.

                                        What I have noticed:

                                        1. Double-Click signal on the Table View calls slot function onTableChanged()
                                        2. onTableChanged() calls data() when it has finished
                                          <>
                                        3. Flags are checked
                                        4. setData() is called if flag is editable
                                        5. Signal dataChanged is emitted by setData()

                                        Where's the problem?

                                        Say I want to allow modification of some cells (name, surname...) and forbid modification of others (primary key, foreign key...)

                                        I have a vector of immutable columns (0, 3, 5...)

                                        I have to check if the column being edited belongs to the vector of immutable columns... TWICE!

                                        One inside the flag() function and one inside onTableChanged()

                                        • Doing the check in flags() keeps me from modifying the content of the cell (hence, disabling the double-click)
                                        • Doing the check in onTableChanged() keeps the cells from being added to the vector of cells that need to be coloured by data()

                                        Both checks are identical, but serve for 2 different purposes: one for disabling the double-click, the other for disabling the coloration.

                                        All of these events are not sequential. I put a symbol <> in the list above to evidence two apparently unrelated events.

                                        If these events had been sequential, I would've written only ONE check at the beginning of the event.

                                        So, the question is: should I keep the situation this way (2 checks)?

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

                                        @devhobby
                                        Yeah, I don't really understand what you're saying, and I don't seem to have the knack of the miraculous correct interpretations @SGaist comes up with :)

                                        If I understand right, you should have two different method overloads doing different things for what you want:

                                        • data(): when called for BackgroundRole, this will look up your vector of changed cells to determine the desired color.

                                        • flags(): when called, this should look up whether this is to be an editable cell, which depends on column (PK/FK versus others), but not your "edited vector". That will determine whether or not it returns Qt::ItemIsEditable. If it's not editable, Qt won't let it get edited.

                                        VRoninV 1 Reply Last reply
                                        0
                                        • JonBJ JonB

                                          @devhobby
                                          Yeah, I don't really understand what you're saying, and I don't seem to have the knack of the miraculous correct interpretations @SGaist comes up with :)

                                          If I understand right, you should have two different method overloads doing different things for what you want:

                                          • data(): when called for BackgroundRole, this will look up your vector of changed cells to determine the desired color.

                                          • flags(): when called, this should look up whether this is to be an editable cell, which depends on column (PK/FK versus others), but not your "edited vector". That will determine whether or not it returns Qt::ItemIsEditable. If it's not editable, Qt won't let it get edited.

                                          VRoninV Offline
                                          VRoninV Offline
                                          VRonin
                                          wrote on last edited by
                                          #63

                                          @JonB There is one more point: setData should actually do something. QSqlQueryModel::setData is just a return false;

                                          "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

                                          JonBJ 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