Remove user selected rows from a QTableWidget
-
wrote on 26 Mar 2013, 21:31 last edited by
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())
@ -
wrote on 26 Mar 2013, 21:33 last edited by
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.
-
wrote on 26 Mar 2013, 21:34 last edited by
[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?
-
wrote on 26 Mar 2013, 21:36 last edited by
Yes, I'm importing both of those. The other widgets successfully use classes from both of those modules.
-
wrote on 26 Mar 2013, 21:43 last edited by
[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)
@ -
wrote on 26 Mar 2013, 21:52 last edited by
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. -
wrote on 26 Mar 2013, 22:09 last edited by
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()
@ -
wrote on 26 Mar 2013, 22:35 last edited by
And one more older connection style to try out :-)
@
QtCore.QObject.connect(deleteButton,QtCore.SIGNAL('clicked()'),remove)
@ -
wrote on 27 Mar 2013, 17:52 last edited by
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.
-
wrote on 28 Mar 2013, 14:37 last edited by
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 indexesqList = 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)
@
-
wrote on 28 Mar 2013, 15:09 last edited by
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) :-)
-
wrote on 17 May 2022, 11:58 last edited byThis post is deleted!