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 change the order of row in QtableWidget.
Qt 6.11 is out! See what's new in the release blog

How to change the order of row in QtableWidget.

Scheduled Pinned Locked Moved Unsolved Qt for Python
10 Posts 5 Posters 2.4k 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.
  • Y Offline
    Y Offline
    yashi95
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Y 2 Replies Last reply
      1
      • SGaistS SGaist

        Hi,

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

        Y Offline
        Y Offline
        yashi95
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

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

          Y Offline
          Y Offline
          yashi95
          wrote on last edited by yashi95
          #4

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

          jazzycamelJ 1 Reply Last reply
          0
          • Y yashi95

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

            jazzycamelJ Offline
            jazzycamelJ Offline
            jazzycamel
            wrote on last edited by
            #5

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

            For the avoidance of doubt:

            1. All my code samples (C++ or Python) are tested before posting
            2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
            Y 1 Reply Last reply
            1
            • jazzycamelJ jazzycamel

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

              Y Offline
              Y Offline
              yashi95
              wrote on last edited by yashi95
              #6

              @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
              
              JonBJ 1 Reply Last reply
              0
              • Y yashi95

                @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
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

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

                Y 1 Reply Last reply
                1
                • JonBJ JonB

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

                  Y Offline
                  Y Offline
                  yashi95
                  wrote on last edited by yashi95
                  #8

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

                  JonBJ jsulmJ 2 Replies Last reply
                  0
                  • Y yashi95

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

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

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

                    1 Reply Last reply
                    0
                    • Y yashi95

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

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by jsulm
                      #10

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

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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