Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-102809

currentRow() and currentColumn() return wrong index values for QTableWidget, when you select more than one item and/or navigate while holding down the mouse button

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.15.10, 6.3.0, 6.x
    • Widgets: Itemviews
    • None
    • Windows 10/11 64BIT, PyQt5 and PyQt6
    • All

    Description

      QTableWidget methods currentRow() and currentColumn() return wrong values when the mouse button is kept pressed (in order to make multiple selections, or simply to navigate through the table content). 

      Reproduction Steps:

      • Launch the code sample below and execute it:
      • When you click on different areas on the table you should see the appropriate INDEX numbers, which is the expected and normal behaviour. 

      BUG Reproduction for currentRow() :

      • Now just click on any area on the table and keep the mouse button pressed while changing the selected area on the table 
      • Down below, you see the currentRow() method returns wrong values (actually it returns the previously selected row's index number) 

      BUG Reproduction for currentColumn() :

      • The currentColumn() method returns wrong values (it returns the previously selected column's index) when you move the mouse cursor at the same row, to the left or to the right columns, while keeping the mouse button pressed.

       


      MORE DETAILS:  Here are some problems caused by this BUG:

      While the mouse button is pressed to change or select the table cell(s);

      • you can never get correct coordinates (row and column) of the current selected cell,
      • if the SelectionBehavior is set to rowSelect to navigate through the table contents at each row, you can never get the correct index of current selected row!

      EXAMPLE SCENARIOS: Just for example scenarios, Users may need/want to keep mouse button pressed while:

      • Navigating through the table contents (the program may use table contents as a navigation menu) or
      • selection of multiple rows/cells/columns of the table
      • etc.

       SAMPLE CODE:

      from PySide2 import QtWidgets, QtCore
      
      class Ui_Form(object):
          def setupUi(self, Form):
              Form.setObjectName("Form")
              Form.resize(600, 500)
              Form.setWindowTitle(QtCore.qVersion())
              self.label = QtWidgets.QLabel(Form)
              self.label.setText("[ Row - Col ]      (Current INDEX)")
              self.lineEdit = QtWidgets.QLineEdit(Form)
              self.tableWidget = QtWidgets.QTableWidget(Form)
              self.tableWidget.setColumnCount(4)
              self.tableWidget.setRowCount(10)
              # Print appropriate INDEX info (Row and Column) in each cell of the table
              for i in range(10):
                  for j in range(4):
                      self.tableWidget.setItem(i, j, QtWidgets.QTableWidgetItem(f"          {i} - {j}"))            
              self.verticalLayout_2 = QtWidgets.QVBoxLayout(Form)
              self.verticalLayout = QtWidgets.QVBoxLayout()
              self.verticalLayout.addWidget(self.tableWidget)
              self.verticalLayout.addWidget(self.label)
              self.verticalLayout.addWidget(self.lineEdit)
              self.verticalLayout_2.addLayout(self.verticalLayout)
      
              ## Uncomment to see the same BUG when the 'SelectionBehavior' is set to 'rowSelect'
              # rowSelect = QtWidgets.QAbstractItemView.SelectRows
              # self.tableWidget.setSelectionBehavior(rowSelect)
              singleSelect = QtWidgets.QAbstractItemView.SingleSelection
              self.tableWidget.setSelectionMode(singleSelect)
      
              # BUG: Wrong INDEX returns by currentRow() and currentColumn() 
              self.tableWidget.itemSelectionChanged.connect(lambda: 
              self.lineEdit.setText( "   " +    
              str(self.tableWidget.currentRow()) + "  ,  " + 
              str(self.tableWidget.currentColumn())))
      
      if __name__ == "__main__":
          import sys
          app = QtWidgets.QApplication(sys.argv)
          Form = QtWidgets.QWidget()
          ui = Ui_Form()
          ui.setupUi(Form)
          Form.show()
          sys.exit(app.exec_())
      

      Attachments

        1. 2022-04-23 164319.png
          2022-04-23 164319.png
          16 kB
        2. 2022-04-23 164351.png
          2022-04-23 164351.png
          16 kB
        3. bugreport_Python_Code.zip
          0.8 kB
        4. bugreport.py
          2 kB
        5. rowSelect.png
          rowSelect.png
          15 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            youngtrms Oguz SAAT
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes