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. Remove user selected rows from a QTableWidget
QtWS25 Last Chance

Remove user selected rows from a QTableWidget

Scheduled Pinned Locked Moved General and Desktop
19 Posts 3 Posters 40.9k 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
    panosk
    wrote on 26 Mar 2013, 20:20 last edited by
    #4

    I quickly copy-pasted your code commenting the rest of your widgets and gave it a try with my edits (using PySide). I only see the widget with the delete button but when I click on it no errors mentioned.

    If possible, post the rest of your code and your imports so I can see the table widget.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SatelliteEyes
      wrote on 26 Mar 2013, 20:50 last edited by
      #5

      Here's the whole code. The widget I'm having trouble with should run independent of the others for now, which is why I didn't include the later ones. Lines 29 - 34 have change, since I've continued to debug my code, of course.

      I need to get the Table to recognize a signal. The "State" protected type on line 29 is from QAbstractItemView. The argument is set to 2 because that indicates that the user is selecting items. I am very new to PyQt, so it is very possible that I am just using this incorrectly, but the description in the documentation (http://doc.qt.digia.com/4.4/qabstractitemview.html#State-enum) suggests that it is at least part of what I need.

      I am getting the error: "insufficient number of arguments to QItemSelectionModel()". I have been reading the documentation about this and will continue to read it long after I post, but I have yet to be able to understand what to put in those parentheses. I believe it is probably a constant or integer which indicates the Selection Mode, but I can't find a list or a table that shows the available built-in modes.

      @
      class questionGUI(QtGui.QWidget):
      #This class is the window of the gui. The methods within this class
      #are the widgets that make up the different user input regions of the gui
      grid = QtGui.QGridLayout()
      grid.setSpacing(10)

      row = 3
      
      def __init__(self):
          super(questionGUI,self).__init__()
      
          self.widgetFive()
          #self.widgetFour()
          self.widgetThree()
          self.widgetTwo()
          self.widgetOne()
      
      def widgetOne(self):
          qTable = QtGui.QTableWidget(self.row,1)
          qTableName = QtGui.QLabel('Available Questions')
          entries = ['[Pick Image] <Default>','[Slider Question] <Default>', '[Comment Box] <Default>']
      
          addrow = 0
          for i in entries:
              item = QtGui.QTableWidgetItem(i)
              qTable.setItem(addrow,0,item)
              addrow += 1
          
          qTable.State(2)
          selection = QtGui.QItemSelectionModel()
      
          select = selection.currentRowChanged()
      
          selected = qTable.selectedItems()
      
          def remove(self):
              #hopefully this will let the Delete button remove the selected table row
              qTable.removeRow(selected)
      
          deleteButton = QtGui.QPushButton('Delete',self)
          deleteButton.connect(deleteButton,QtCore.SIGNAL('clicked()'),remove)
      
          qTable.setColumnWidth(0,300)
      
          self.grid.addWidget(qTableName,1,0)
          self.grid.addWidget(qTable,2,0, 1, 2)
          self.grid.addWidget(deleteButton,3,0)
      
      def widgetTwo(self):
          #this widget is composed of a pulldown menu with the different question type options.
          qType = QtGui.QLabel('Type')
          qTypeMenu = QtGui.QComboBox()
          qTypeMenu.addItem('[Pick Image] <customtext>')
          qTypeMenu.addItem('[Slider Question] <customtext>')
          qTypeMenu.addItem('[Comment Box] <customtext>')
      
          self.grid.addWidget(qType, 4, 0)
          self.grid.addWidget(qTypeMenu,4,1)
      
      def widgetThree(self):
          #this widget allows you to edit the Question text and the Tab text.
          qText = QtGui.QLabel('Question Text')
          qTextEntry = QtGui.QLineEdit()
          tText = QtGui.QLabel('Tab Text')
          tTextEntry = QtGui.QLineEdit()
      
          self.grid.addWidget(qText, 5,0)
          self.grid.addWidget(qTextEntry, 5, 1)
          self.grid.addWidget(tText, 6, 0)
          self.grid.addWidget(tTextEntry, 6, 1)
      
      def widgetFive(self):
          addQuestionButton = QtGui.QPushButton('Add Question',self)
          addQuestionButton.connect(addQuestionButton,QtCore.SIGNAL('clicked()'),QtGui.qApp, QtCore.SLOT('quit()'))
          addQuestionButton.resize(addQuestionButton.sizeHint())
      
          
          doneButton = QtGui.QPushButton('Done',self)
          doneButton.connect(doneButton,QtCore.SIGNAL('clicked()'),QtGui.qApp, QtCore.SLOT('quit()'))
          doneButton.resize(doneButton.sizeHint())
      
          self.grid.addWidget(addQuestionButton,7,0)
          self.grid.addWidget(doneButton,7,1)
      
          self.setLayout(self.grid)
      
          self.setGeometry(300,300,400,500)
          self.setWindowTitle('Question Editor')
          self.show()
      

      def main():
      app = QtGui.QApplication(sys.argv)
      ex = questionGUI()
      sys.exit(app.exec_())

      if name == 'main':
      main()
      @

      1 Reply Last reply
      0
      • P Offline
        P Offline
        panosk
        wrote on 26 Mar 2013, 21:05 last edited by
        #6

        You don't need selection models for this simple operation. Do your connection like this and it will work:
        @
        ...
        qTable.State(2)
        #selection = QtGui.QItemSelectionModel()

            #select = selection.currentRowChanged()
        
            #selected = qTable.selectedItems()
        
            def remove():
                #hopefully this will let the Delete button remove the selected table row
                qTable.removeRow(qTable.currentRow())
        
            deleteButton = QtGui.QPushButton('Delete',self)
            #deleteButton.connect(deleteButton,QtCore.SIGNAL('clicked()'),remove)
            deleteButton.clicked.connect(remove)
        

        ...
        @

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SatelliteEyes
          wrote on 26 Mar 2013, 21:31 last edited by
          #7

          This edit gives me the error: TypeError: remove() takes exactly 1 argument (0 given). If it matters, this error occurs after I open the GUI, select a row and click "Delete".

          That's why I've been trying to figure out how to make a variable that translates a signal into some sort of integer value, preferably the row index, which can be passed to remove(). Clearly, my clicking is not creating the argument type that remove needs.

          The argument needed is, in your code, 'currentRow()'. Is it a default of QTableWidget to change the currentRow as the user makes selections? That's what your code suggests, but my errors suggest that the remove function is not seeing any row changes as an argument.

          If I print qTable.currentRow() before any selections are made, it has a value of -1. This makes sense because none of the rows are selected by default. I think that this must not be getting passed to remove because removeRow(-1) should give some sort of 'index value out of range' error and not a 'no argument given' error.

          If I try to pass something to remove explicitly:
          @
          selected = qTable.currentRow()

          def remove(selected,self):
          qTable.removeRow(selected)

          deleteButton = QtGui.QPushButton('Delete',self)
          deleteButton.connect(deleteButton, QtCore.SIGNAL('clicked()'),remove(selected))
          @

          I get the error "TypeError: argument 3 of QObject.connect() has an invalid type"

          So remove will work as the SLOT but only if it does not call anything. That will trigger an error about it not getting passed enough arguments. If remove has anything between the parentheses, it triggers an invalid type error.

          I can not use the syntax you provided for deleteButton.connect because it was introduced in a later version of PyQt. My version is 4.4.4 and I do not have the ability to upgrade because this is a work project and work tells me what version to use. My syntax work just fine for all of the other buttons, it's just less elegant.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            panosk
            wrote on 26 Mar 2013, 21:31 last edited by
            #8

            Btw, if your intention with the selection models was to remove multiple selected rows, then use sth like this
            @
            def remove():
            rows = qTable.selectionModel().selectedRows()
            for r in rows:
            qTable.removeRow(r.row())
            @

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SatelliteEyes
              wrote on 26 Mar 2013, 21:33 last edited by
              #9

              I might have to add that functionality in the future, though I doubt that the users will need to remove more than one row at a time. Right now, I just need one row to be deleted.

              1 Reply Last reply
              0
              • P Offline
                P Offline
                panosk
                wrote on 26 Mar 2013, 21:34 last edited by
                #10

                [quote author="SatelliteEyes" date="1364333501"]This edit gives me the error: TypeError: remove() takes exactly 1 argument (0 given). If it matters, this error occurs after I open the GUI, select a row and click "Delete".[/quote]

                I tested the code before posting and it's working just fine on my setup. Are you importing the correct modules? QtGui, QtCore, etc?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SatelliteEyes
                  wrote on 26 Mar 2013, 21:36 last edited by
                  #11

                  Yes, I'm importing both of those. The other widgets successfully use classes from both of those modules.

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    panosk
                    wrote on 26 Mar 2013, 21:43 last edited by
                    #12

                    [quote author="SatelliteEyes" date="1364333501"]I can not use the syntax you provided for deleteButton.connect because it was introduced in a later version of PyQt. My version is 4.4.4 and I do not have the ability to upgrade because this is a work project and work tells me what version to use. My syntax work just fine for all of the other buttons, it's just less elegant.
                    [/quote]

                    Then fix your connection like this:
                    @
                    self.connect(deleteButton,QtCore.SIGNAL('clicked()'),remove)
                    @

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SatelliteEyes
                      wrote on 26 Mar 2013, 21:52 last edited by
                      #13

                      I changed what I already had:
                      @ deleteButton.connect(deleteButton,QtCore.SIGNAL('clicked()'),remove)
                      @

                      to:
                      @ self.connect(deleteButton,QtCore.SIGNAL('clicked()'),remove)@

                      and I am still getting the same error.

                      I thought that it might have something to do with not specifying a receiver, so I changed things to:
                      @ deleteButton.connect(deleteButton,QtCore.SIGNAL('clicked()'),qTable,remove())
                      @
                      This continues to give me the error that remove is not receiving arguments.

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        panosk
                        wrote on 26 Mar 2013, 22:09 last edited by
                        #14

                        Don't know what's wrong then. The button code works fine on my setup (using PySide instead of PyQt though), with both connection styles. Anyway, here is the full code that works for me. Try to run it as is and see if the button works. Otherwise, you may have to dig deeper into your PyQt version.
                        @
                        class questionGUI(QtGui.QWidget):
                        #This class is the window of the gui. The methods within this class
                        #are the widgets that make up the different user input regions of the gui
                        grid = QtGui.QGridLayout()
                        grid.setSpacing(10)

                        row = 3
                        
                        def __init__(self):
                            super(questionGUI,self).__init__()
                        
                            self.widgetFive()
                            #self.widgetFour()
                            self.widgetThree()
                            self.widgetTwo()
                            self.widgetOne()
                            
                        
                        def widgetOne(self):
                            qTable = QtGui.QTableWidget(self.row,1)
                            qTableName = QtGui.QLabel('Available Questions')
                            entries = ['[Pick Image] <Default>','[Slider Question] <Default>', '[Comment Box] <Default>']
                        
                            addrow = 0
                            for i in entries:
                                item = QtGui.QTableWidgetItem(i)
                                qTable.setItem(addrow,0,item)
                                addrow += 1
                           
                            qTable.State(2)
                        
                            def remove():
                                rows = qTable.selectionModel().selectedRows()
                                for r in rows:
                                    qTable.removeRow(r.row())
                        
                            deleteButton = QtGui.QPushButton('Delete',self)
                            self.connect(deleteButton,QtCore.SIGNAL('clicked()'),remove)
                        
                            qTable.setColumnWidth(0,300)
                        
                            self.grid.addWidget(qTableName,1,0)
                            self.grid.addWidget(qTable,2,0, 1, 2)
                            self.grid.addWidget(deleteButton,3,0)
                        
                        def widgetTwo(self):
                            #this widget is composed of a pulldown menu with the different question type options.
                            qType = QtGui.QLabel('Type')
                            qTypeMenu = QtGui.QComboBox()
                            qTypeMenu.addItem('[Pick Image] <customtext>')
                            qTypeMenu.addItem('[Slider Question] <customtext>')
                            qTypeMenu.addItem('[Comment Box] <customtext>')
                        
                            self.grid.addWidget(qType, 4, 0)
                            self.grid.addWidget(qTypeMenu,4,1)
                        
                        def widgetThree(self):
                            #this widget allows you to edit the Question text and the Tab text.
                            qText = QtGui.QLabel('Question Text')
                            qTextEntry = QtGui.QLineEdit()
                            tText = QtGui.QLabel('Tab Text')
                            tTextEntry = QtGui.QLineEdit()
                        
                            self.grid.addWidget(qText, 5,0)
                            self.grid.addWidget(qTextEntry, 5, 1)
                            self.grid.addWidget(tText, 6, 0)
                            self.grid.addWidget(tTextEntry, 6, 1)
                        
                        def widgetFive(self):
                            addQuestionButton = QtGui.QPushButton('Add Question',self)
                            addQuestionButton.connect(addQuestionButton,QtCore.SIGNAL('clicked()'),QtGui.qApp, QtCore.SLOT('quit()'))
                            addQuestionButton.resize(addQuestionButton.sizeHint())
                        
                           
                            doneButton = QtGui.QPushButton('Done',self)
                            doneButton.connect(doneButton,QtCore.SIGNAL('clicked()'),QtGui.qApp, QtCore.SLOT('quit()'))
                            doneButton.resize(doneButton.sizeHint())
                        
                            self.grid.addWidget(addQuestionButton,7,0)
                            self.grid.addWidget(doneButton,7,1)
                        
                            self.setLayout(self.grid)
                        
                            self.setGeometry(300,300,400,500)
                            self.setWindowTitle('Question Editor')
                            self.show()
                        

                        def main():
                        app = QtGui.QApplication(sys.argv)
                        ex = questionGUI()
                        sys.exit(app.exec_())

                        if name == 'main':
                        main()
                        @

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          panosk
                          wrote on 26 Mar 2013, 22:35 last edited by
                          #15

                          And one more older connection style to try out :-)
                          @
                          QtCore.QObject.connect(deleteButton,QtCore.SIGNAL('clicked()'),remove)
                          @

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SatelliteEyes
                            wrote on 27 Mar 2013, 17:52 last edited by
                            #16

                            Thanks for your help yesterday. After all this, I'm pretty sure it has to be a version problem. Your code is still giving me the 'not enough arguments' error.

                            Just to be safe, I change my table to a list, thinking that maybe a simpler widget wouldn't have the problem, but it does. I think I'm going to give up on this and find someone at my company to debug it the rest of the way with me. Thanks,again.

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SatelliteEyes
                              wrote on 28 Mar 2013, 14:37 last edited by
                              #17

                              I've made progress! I'm not sure if you're still interested, but here is the development. I'm using QtCore.SIGNAL('itemClicked(clicked)') as the signal, rather than the regular QtCore.SIGNAL('clicked()'). This still does not delete the line, but it is not giving me the 'no arguments' error. A new error is always good!

                              Now, when I select a row, the selection is blue. When I click the Delete button, the selection color (not the text, the background color in the selected cell) turns grey. You don't have to respond to this, I'll beat on this some more and post a new discussion if I get stuck.

                              Here is the new code:

                              @
                              def widgetOne(self):
                              def questionMoved(self,indexes):
                              print indexes

                                  qList = QtGui.QListWidget(self)
                                  qList.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
                                  qList.connect(qList,QtCore.SIGNAL('indexesMoved(const QModelIndexList&)'),questionMoved)
                              
                                  qListName = QtGui.QLabel('Available Questions')
                                  entries = ['[Pick Image] <Default>','[Slider Question] <Default>', '[Comment Box] <Default>']
                              
                                  for i in entries:
                                      item = QtGui.QListWidgetItem(i)
                                      qList.addItem(item)
                                  
                                  def deleteLink(self):
                                      self._sql.delete()
                                      self._sql = None
                              
                                  def remove(self):
                                      #hopefully this will let the Delete button remove the selected table row
                                      row = qList.currentRow()
                                      if (row<0): return
                              
                                      item = qList.takeItem(row)
                                      item.deleteLink()
                              
                                      for r in range(row, qList.count()):
                                          item = qList.item(r)
                                          item.setOrder(row)
                                          row += 1
                                  
                                  clicked = qList.currentRow()
                                  deleteButton = QtGui.QPushButton('Delete',self)
                                  deleteButton.connect(deleteButton,QtCore.SIGNAL('itemClicked(clicked)'),remove)
                              
                                  self.grid.addWidget(qListName,1,0)
                                  self.grid.addWidget(qList,2,0, 1, 2)
                                  self.grid.addWidget(deleteButton,3,0)
                              

                              @

                              1 Reply Last reply
                              0
                              • P Offline
                                P Offline
                                panosk
                                wrote on 28 Mar 2013, 15:09 last edited by
                                #18

                                Hi there,

                                In case you haven't already seen this, have a look at this page http://pyqt.sourceforge.net/Docs/PyQt4/old_style_signals_slots.html#pyqt4-signals-and-qt-signals

                                My advice though would be to convince your team to use a more recent version of PyQt (or use PySide) :-)

                                1 Reply Last reply
                                0
                                • Jimmy CrabJ Offline
                                  Jimmy CrabJ Offline
                                  Jimmy Crab
                                  wrote on 17 May 2022, 11:58 last edited by
                                  #19
                                  This post is deleted!
                                  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