How to change the order of row in QtableWidget.
-
@SGaist
Can you please help me how to replace "setItem(row, col, rowItems.at(col));" line in pyside2(python)?I don't have idea about "rowItems.at(col))".
-
@jazzycamel As per your suggesation i tried but i am getting bellow Error
self.studentTable.setItem(rowno, colmn, rowItems[colmn])
TypeError: 'PySide2.QtWidgets.QTableWidgetItem' object is not subscriptableBelow you can check my code which I tried. Please help me in this.
checkIndex = 0 indexes = [QPersistentModelIndex( index) for index in self.studentTable.selectionModel().selectedRows()] for index in indexes: selIndex = index.row() insertIndex = selIndex - 1 if selIndex != checkIndex and self.sharpOsaApplicationTable.item(0,0) is not None: sourceItems = self.takeRow(selIndex) destItems = self.takeRow(insertIndex) self.setRow(selIndex, destItems) self.setRow(insertIndex, sourceItems)
def takeRow(self, rowno): colmn = 0 rowItems = [] while colmn < self.studentTable.columnCount(): rowItems = self.studentTable.takeItem(rowno, colmn) colmn = colmn + 1 print (rowItems) return rowItems def setRow(self, rowno, rowItems): colmn = 0 print(rowItems) while colmn < self.studentTable.columnCount(): self.studentTable.setItem(rowno, colmn, rowItems[colmn]) colmn = colmn + 1
-
@yashi95 said in How to change the order of row in QtableWidget.:
rowItems = self.studentTable.takeItem(rowno, colmn)
How does this build an array/list in Python? It doesn't. And your
print (rowItems)
statement shows you that. So since yourrowItems
/return result is not a list/array, that's why later you get "not subscriptable" onrowItems[colmn]
,rowItems
being a singleQTableWidgetItem
.This is not a Qt issue, just a Python one.
-
@yashi95 said in How to change the order of row in QtableWidget.:
Please can you suggest to me what I will do?
You will hopefully read documentation: https://docs.python.org/3/tutorial/datastructures.html
(hint: list.append(x))
And also keep in mind that Python is not strongly typed. The fact that you first assign an empty list to rowItems doesn't mean it will stay a list and you assign a value to it which is not a list as @JonB already explained.