Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Issues with setting checkboxes in QSqlTableModel
Forum Updated to NodeBB v4.3 + New Features

Issues with setting checkboxes in QSqlTableModel

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 2 Posters 1.3k 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.
  • - Offline
    - Offline
    -KRS-
    wrote on last edited by
    #1

    I have subclassed QSqlTableModel so I can add checkboxes to a QTableView. I've done this by overriding the flags(), data() and setData() methods. When the tableview loads the data from SQL it seems to work fine and does set the checkboxes accordingly. But when I click on the checkboxes nothing happens to either the data in the database, or the checkbox in the view.

    Here's what I'm doing currently in my setData method:

        def setData(self, index, value, role=Qt.EditRole):
            if role == Qt.CheckStateRole\
                    and self.headerData(index.column(), Qt.Horizontal) in \
                    ("Game", "Console", "Accessory", "Box", "Manual"):
                data = "Yes" if value == Qt.Checked else "No"
                return QSqlTableModel.setData(self, index, data, Qt.EditRole)
            else:
                return QSqlTableModel.setData(self, index, value, role)
    

    Basically I'm checking if the role is CheckStateRole and if the column is in the column I have the checkboxes in. Then checking the value and passing the correct data to the database based on it.

    It does enter the if statement, and checking the value of data seems like it does set it correctly. So the part that doesn't work is the call to QSqlTableModel.setData() in the return statement. It doesn't seem to do anything.

    Editing columns that don't have checkboxes works fine.

    Am I doing something obviously wrong here?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      I'm not a python expert but QSqlTableModel.setData(self, index, data, Qt.EditRole) looks wrong. To call the base class implementation you should call something like super(QSqlTableModel,self).setData(self, index, data, Qt.EditRole)

      "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
      • VRoninV VRonin

        I'm not a python expert but QSqlTableModel.setData(self, index, data, Qt.EditRole) looks wrong. To call the base class implementation you should call something like super(QSqlTableModel,self).setData(self, index, data, Qt.EditRole)

        - Offline
        - Offline
        -KRS-
        wrote on last edited by -KRS-
        #3

        @VRonin I'm pretty new to python myself, as well as QT, so I'm admittedly a bit unsure as to the difference between what I was doing and using super like that. Thanks for pointing it out. I should look into it more.

        I changed the whole subclass to use super() but it still has the same behavior unfortunately. :(

        Edit: Actually now I can't edit any other column either. I guess that makes it consistent at least. But it doesn't make sense. Reading up on super() the way I was doing it is simply how you do it without using the super function, and the way it was done before super was introduced in Python. The behavior should be identical if I'm reading it correctly. But obviously something changed. Maybe someone can chime in on that.

        Edit²: Ok never mind the previous edit. Changing super(QSqlTableModel, self) into just super() everywhere (for example super().setData(index, data, Qt.EditRole) made the behavior exactly as when I used QSqlTableModel to call the base implementation. That is, I can edit the columns that don't have checkboxes but the checkboxes are still not co-operating.

        1 Reply Last reply
        0
        • - Offline
          - Offline
          -KRS-
          wrote on last edited by
          #4

          So I took a look at the QSqlTableModel C++ source code, and I noticed that there's an if statement in there that checks if the index has the flag ItemIsEditable set. If it isn't, then it returns false.

          Now in the example I was adapting from (Which was this one that is subclassing a QSortFilterProxyModel rather than QSqlTableModel, and was using boolean values and doing other things as well) did NOT set that flag in the overridden flags() method! I assume that's done somewhere else, but I only adapted what I needed. I didn't even know it was a flag you could set since I've never really done this before. Should've looked more carefully at the example I guess...

          Anyway, after setting that flag in the flags method it now works as expected. Here's what the method is looking like now if anyone is interested:

              def flags(self, index):
                  if self.headerData(index.column(), Qt.Horizontal) in ("Game", "Console", "Accessory",
                                                                        "Box", "Manual"):
                      return Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable
                  else:
                      return super().flags(index)
          

          You can also set the return statement to simply:

          return super().flags(index) | Qt.ItemIsUserCheckable
          

          And it'll work just the same. That's actually probably a better/cleaner way of doing it now that I think about it.

          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