Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to change the order of row in QtableWidget.



  • Hi,

    I want to know if we have some information in QtableWidget, and I have one up and down button so, how I will change the order of row while clicking on the up and down button.


  • Lifetime Qt Champion

    Hi,

    Here you have a pretty simple implementation that you can take inspiration from.



  • @SGaist
    Thank you. I am a beginner in python and Here code is written in c++. Is a little bit difficult for me to understand. Can you please provide me with python? it will very helpful.



  • @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))".



  • @yashi95 In this example rowItems is a QList which in python would be simply be a list, so rowItems.at(col) in C++ would be rowItems[col] in python.



  • @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 subscriptable

    Below 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 your rowItems/return result is not a list/array, that's why later you get "not subscriptable" on rowItems[colmn], rowItems being a single QTableWidgetItem.

    This is not a Qt issue, just a Python one.



  • @JonB But I make it rowItems as list also
    rowItems = []
    while colmn < self.studentTable.columnCount():
    rowItems = self.studentTable.takeItem(rowno, colmn)

    Please can you suggest to me what I will do?



  • @yashi95
    If you do not know how to append/add/insert items to a list/array in Python you won't get very far, you need to go find out how to do so.


  • Lifetime Qt Champion

    @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.


Log in to reply