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. How to enable checkbox activity in TableView
Forum Updated to NodeBB v4.3 + New Features

How to enable checkbox activity in TableView

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

    Hi!
    In PyQt, I made checkboxes in the TableModel using this flag (column 1):

        def data(self, index, role=None):
            if   index.column() == 0 and role == 0:
                return self.id[index.row()]
            elif index.column() == 1 and role == 10:
                return self.checked[index.row()]
            elif index.column() == 2 and role == 0:
                return self.nameCd[index.row()]
            return None
    

    How to enable checkbox activity in TableView?
    If I do this, the program crashes on the penultimate line

        def flags(self, index):
            result = super(ModelCdViewer, self).flags(index)
            print(result)
            if index.column() == 1 :
                result |= Qt.ItemIsUserCheckable
            return result
    

    It is too not works:

    self.ui.tableView.setEditTriggers(QtWidgets.QAbstractItemView.AllEditTriggers)
    
    JonBJ 1 Reply Last reply
    0
    • M Mikeeeeee

      Hi!
      In PyQt, I made checkboxes in the TableModel using this flag (column 1):

          def data(self, index, role=None):
              if   index.column() == 0 and role == 0:
                  return self.id[index.row()]
              elif index.column() == 1 and role == 10:
                  return self.checked[index.row()]
              elif index.column() == 2 and role == 0:
                  return self.nameCd[index.row()]
              return None
      

      How to enable checkbox activity in TableView?
      If I do this, the program crashes on the penultimate line

          def flags(self, index):
              result = super(ModelCdViewer, self).flags(index)
              print(result)
              if index.column() == 1 :
                  result |= Qt.ItemIsUserCheckable
              return result
      

      It is too not works:

      self.ui.tableView.setEditTriggers(QtWidgets.QAbstractItemView.AllEditTriggers)
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Mikeeeeee said in How to enable checkbox activity in TableView:

      elif index.column() == 1 and role == 10:

      Why 10?

      The def flags() is what you need to enable checkboxes. Your code looks good to me. Try it again? :)

      M 1 Reply Last reply
      0
      • JonBJ JonB

        @Mikeeeeee said in How to enable checkbox activity in TableView:

        elif index.column() == 1 and role == 10:

        Why 10?

        The def flags() is what you need to enable checkboxes. Your code looks good to me. Try it again? :)

        M Offline
        M Offline
        Mikeeeeee
        wrote on last edited by
        #3

        @JonB 10 because this is how the checkbox appears in the cell with true/false.
        How do I enable cell editing?

        JonBJ 1 Reply Last reply
        0
        • M Mikeeeeee

          @JonB 10 because this is how the checkbox appears in the cell with true/false.
          How do I enable cell editing?

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

          @Mikeeeeee
          OK, now I understand Qt.CheckStateRole == 10, might be better if you used the constant like you did for Qt.ItemIsUserCheckable.

          A moment ago you were asking

          How to enable checkbox activity in TableView?

          Now you say

          How do I enable cell editing?

          What is your question?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mikeeeeee
            wrote on last edited by
            #5

            Me need enable checkbox activity in TableView.
            If I do this with checkboxes, then after clicking on the checkbox, the program breaks. Probably after changing the checkbox.

                def flags(self, index):
                    result = super(ModelCdViewer, self).flags(index)
                    print(result)
                    if index.column() == 1 :
                        result |= QtCore.Qt.ItemIsUserCheckable
                        # result |= QtCore.Qt.ItemIsEditable
                    return result
            

            If I do this with text input, then after changing the text, the program breaks down

                def flags(self, index):
                    result = super(ModelCdViewer, self).flags(index)
                    print(result)
                    if index.column() == 2 :
                        result |= QtCore.Qt.ItemIsEditable
                    return result
            

            How do I save checkbox values?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mikeeeeee
              wrote on last edited by Mikeeeeee
              #6

              I tried to do this, but the program still crashes. What else does the program need?

                  def setData(self, index, value, role=QtCore.Qt.EditRole):
                      if not index.isValid():
                          return False
              
                      if index.column() == 1:
                          print(value)
                          print(self.checked[index.row()])
                          if self.checked[index.row()]:
                              self.checked[index.row()] = False
                          else:
                              self.checked[index.row()] = True
                          print(self.checked[index.row()])
                          
                          # self.checked[index.row()] = value
                          self.dataChanged().emit(index, index)
                          return True
              
                      return False
              
              M JonBJ 2 Replies Last reply
              0
              • M Mikeeeeee

                I tried to do this, but the program still crashes. What else does the program need?

                    def setData(self, index, value, role=QtCore.Qt.EditRole):
                        if not index.isValid():
                            return False
                
                        if index.column() == 1:
                            print(value)
                            print(self.checked[index.row()])
                            if self.checked[index.row()]:
                                self.checked[index.row()] = False
                            else:
                                self.checked[index.row()] = True
                            print(self.checked[index.row()])
                            
                            # self.checked[index.row()] = value
                            self.dataChanged().emit(index, index)
                            return True
                
                        return False
                
                M Offline
                M Offline
                Mikeeeeee
                wrote on last edited by
                #7

                it is work

                    def flags(self, index):
                        result = super(ModelCdViewer, self).flags(index)
                        if index.column() == 1 :
                            result |= QtCore.Qt.ItemIsUserCheckable
                            # result |= QtCore.Qt.ItemIsEditable
                        return result
                
                    def setData(self, index, value, role=QtCore.Qt.EditRole):
                        if not index.isValid():
                            return False
                
                        if index.column() == 1:
                            print(value)
                            print(self.checked[index.row()])
                
                            if self.checked[index.row()]:
                                self.checked[index.row()] = False
                            else:
                                self.checked[index.row()] = True
                
                            print(self.checked[index.row()])
                
                            # self.checked[index.row()] = value
                            # self.dataChanged().emit(index, index)
                            return True
                
                        return False
                
                1 Reply Last reply
                0
                • M Mikeeeeee

                  I tried to do this, but the program still crashes. What else does the program need?

                      def setData(self, index, value, role=QtCore.Qt.EditRole):
                          if not index.isValid():
                              return False
                  
                          if index.column() == 1:
                              print(value)
                              print(self.checked[index.row()])
                              if self.checked[index.row()]:
                                  self.checked[index.row()] = False
                              else:
                                  self.checked[index.row()] = True
                              print(self.checked[index.row()])
                              
                              # self.checked[index.row()] = value
                              self.dataChanged().emit(index, index)
                              return True
                  
                          return False
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Mikeeeeee said in How to enable checkbox activity in TableView:

                  self.dataChanged().emit(index, index)

                  This is wrong. Should be

                  self.dataChanged.emit(index, index)

                  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