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 25.4k 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
    #37

    Your CPP file contains an error, the default value should be in the declaration of the function.

    If you are using Qt >= 5.7, you can simply use the override keyword as C++11 support is mandatory since that version. Q_DECL_OVERRIDE was used to allow compatibly with non-C++11 enabled compilers.

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

    JonBJ devhobbyD 2 Replies Last reply
    2
    • SGaistS SGaist

      Your CPP file contains an error, the default value should be in the declaration of the function.

      If you are using Qt >= 5.7, you can simply use the override keyword as C++11 support is mandatory since that version. Q_DECL_OVERRIDE was used to allow compatibly with non-C++11 enabled compilers.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #38

      @SGaist You're a hero ;-) It takes you like 1 minute to spot it & reply!

      1 Reply Last reply
      1
      • SGaistS SGaist

        Your CPP file contains an error, the default value should be in the declaration of the function.

        If you are using Qt >= 5.7, you can simply use the override keyword as C++11 support is mandatory since that version. Q_DECL_OVERRIDE was used to allow compatibly with non-C++11 enabled compilers.

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

        @SGaist said in QSqlRelationalTableModel with multiple Joins?:

        Your CPP file contains an error, the default value should be in the declaration of the function.

        If you are using Qt >= 5.7, you can simply use the override keyword as C++11 support is mandatory since that version. Q_DECL_OVERRIDE was used to allow compatibly with non-C++11 enabled compilers.

        I hoped it was just that...

        0_1518120023429_f174cbae-ee6a-42fa-b713-287d8d891eff-image.png

        I moved the default value in the header file, same issue

        [ I will update the code posted above so that I don't repost it everytime ]

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

          Did you try re-running qmake before building ?

          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

            Did you try re-running qmake before building ?

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

            @SGaist

            Did you try re-running qmake before building ?

            Thank you, sir! That solved it :)

            @JonB

            I'm going back to your very original post and thinking about what you're actually trying to achieve anyway. The code we're talking about is all well & good, if you want to proceed, but I'm wondering about your expectations of the interface.

            Back to the original post... at the moment I can successfully double-click items and edit them.

            How they are edited and how the edit is going to affect the table and the database is something I believe I can do by myself now.

            I guess I've caught the mechanism behind it!

            BUT

            One thing that has remained unresolved is the fact that anytime I change a field (setData() is called) the edited cell must change its background color.

            I still don't get this one.

            P.S: @JonB I wanted to thank you too. It's amazing the way you're helping me. I just can't thank you enough :)

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

              Do you mean you want to mark the cell as "edited" ?

              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

                Do you mean you want to mark the cell as "edited" ?

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

                @SGaist said in QSqlRelationalTableModel with multiple Joins?:

                Do you mean you want to mark the cell as "edited" ?

                Yes but... my way!

                I want to change it's background color to, say, Cyan.

                This way the user, before committing the changes, can take a look and see what is going to really change in the database.

                1 Reply Last reply
                0
                • 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 Online
                                  JonBJ Online
                                  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 Online
                                      JonBJ Online
                                      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 Online
                                          JonBJ Online
                                          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

                                          • Login

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