Important: Please read the Qt Code of Conduct -

currentRow() in QTableWidget is returning wrong row # when user clicks on a QComboBox

  • Code is in PYQT5
    From what I understand this is a known issue but still having trouble trying to figure this out.

    I have a very simple QTableWidget where I have 5 rows and 5 columns. First column is a QComboBox, whenever I click on it I have it connected as per below. But inside that function call when I try the below code, I never get the correct row number. I'm assuming I have to set the row in the table myself but unsure how to do so, was hoping to do it inside comboChanged(). Any thoughts?

    for index in range(rowCount):
    combo = QtWidgets.QComboBox()
    combo.currentIndexChanged.connect(self.comboChanged) #sends index
    for t in comboBoxWeight:
    combo.addItem(t[0], QVariant(t[1]))

    def comboChanged(self, index):
    print('Combo Changed Index: ' + str(index)) #working fine
    print('Row: ' + str(self.tableWidgetCode.currentRow())) #returning incorrect row number

  • Hi,
    the emit is probably executed before setCurrentRow is called. What's the problem with using the index argument?

  • index is returning me which element in the combo is selected which is fine but I need to figure out what row I'm on because I want to fire another call on that same row the user modified the combo on....

  • I guess what I'm asking is since I'm setting up the combo at this point, I know which row it is, is there any way I can pass the row number here in my code somehow?


  • Lifetime Qt Champion


    It's not an issue per-se however putting a widget in a cell means that you are ignoring what's under that cell.

    What you can do is use a QSignalMapper to map the row number with the QComboBox.

  • @SGaist I was reading about using that for this situation, very new to QT. Any chance of a basic example of how to pass the row number?

  • Lifetime Qt Champion

    Do you mean an example of using setMapping ?

  • @SGaist Honestly, not sure. This is kind of confusing. Trying to send the current row to the currentIndexChanged so I know which row was modified by user.

  • Lifetime Qt Champion

    It's the other way around, you map the QComboBox to the row you'll put it in and then use the mapped signal to retrieve the combo box you just modified and act accordingly.

  • @SGaist Can you show an example on how I would do that?

  • Lifetime Qt Champion

    WARNING From the top of my head, not tested however it should give you an idea on how to proceed further.

    mapper = QSignalMapper()
    for index in range(rowCount):
        combo = QtWidgets.QComboBox()
        // add your code
        mapper.setMapping(combo, index)
        mapper.setMapping(combo, 1)
    def handleCombo(self, index):
        //update the model with the content of the combo box

  • @SGaist still can't get this to work. Example on the site is showing that we have to still use SIGNAL and SLOT but I didn't think that was necessary with QT5....

  • Lifetime Qt Champion

    You using python so there will be things that are different. Indeed Qt 5 introduced a new syntax however there are some few corner cases that might not be supported by it. IIRC argument with default values are not.

    In any case, what did you try and what error did you got ?

  • @SGaist just tried your code, compiles but handlecombo is not being called when I change index in combo box.

    I also see reference to pyqtSignal(), not sure if I need to be using this....

  • Lifetime Qt Champion

    Did you make the mapper a class member or is it only a local variable to the method that does the setup ?

  • @SGaist Just for testing purposes, I created a small .py file and left it as a global variable (I know this isn't the correct way to do it but just wanted to remove the other code.)

  • Lifetime Qt Champion

    Can you share your test code ? Like I wrote, mine was written from memory only.

  • @SGaist
    from PyQt5 import QtGui # Import the PyQt5 module we'll need
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    #from PyQt5.QtWidgets import QMainWindow, QApplication
    from PyQt5 import QtWidgets #import QMainWindow, QApplication
    from PyQt5.QtWidgets import *
    #from PyQt5.QtCore import pyqtSlot, pyqtSignal
    from PyQt5.QtCore import pyqtSignal, pyqtSlot
    import binascii
    import datetime
    import cx_Oracle
    import sys # We need sys so that we can pass argv to QApplication
    import functools
    import codeTable # This file holds our MainWindow and all editor related things
    import os # For listing directory methods

    class CodeApp(QtWidgets.QMainWindow, codeTable.Ui_MainWindow):
    def init(self):
    super(self.class, self).init()
    self.setupUi(self) # This is defined in file automatically
    #mapper = pyqtSignal()

        #mapper = QSignalMapper()
    def getData(self):
        mapper = QSignalMapper()
        rowCount = 5 #len(allSQLRows)
        for index in range(rowCount):
            combo = QtWidgets.QComboBox()
            mapper.setMapping(combo, index)
            mapper.setMapping(combo, 1)
            combo.addItem('AAA', 'aaa')
            combo.addItem('BBB', 'bbb')
            combo.addItem('CCC', 'ccc')
            #print('Index: ' + str(index))    
    def handleCombo(self, index):
            print('handleCombo: ' + str(index))
            print('Row: ' + str(self.tableWidgetCode.currentRow()))
            #print('Row: ' + str(row))
            #update the model with the content of the combo box    
    def closeWindow(self):
    def setPosLists(self):
        global codeHdrList
    def setColumnHeadings(self):
        #table.setHorizontalHeaderLabels(('Col 1', 'Col 2', 'Col 3'))
        #comboBoxWeight = ["0-154 (0)","155-169 (1)","170-184 (2)","185-199 (3)","200-214 (4)","215-229 (5)","230-244 (6)","245-259 (7)","260-274 (8)","275-289 (9)","290-304 (A)","305-319 (B)","320-334 (C)","335-349 (D)","350-364 (E)","365+ (F)"]
        #Other Attributes
        #comboBoxRat = ["0-12 (0)","13-18 (1)","19-24 (2)","25-30 (3)","31-36 (4)","37-42 (5)","43-48 (6)","49-54 (7)","55-60 (8)","61-66 (9)","67-72 (A)","73-78 (B)","79-84 (C)","85-90 (D)","91-96 (E)","97-99 (F)"]
        #self.tableWidgetCode.setHorizontalHeaderLabels(('Code', 'Description', 'Short Description', 'Created By', 'Created On', 'Last Updated By', 'Last Updated On', 'Database ID', 'Deleted B', 'Parent Code', 'Display Seq No', 'Effective Date', 'Expiration Date', 'Code PK', 'Shipping Agent PK'))
        self.tableWidgetCode.setHorizontalHeaderLabels(('Code', 'Description', 'Parent Code', 'Display Seq No', 'Immediate Parent', 'Code PK', 'Short Description', 'Shipping Agent PK'))
        #self.tableWidgetCode.setHorizontalHeaderLabels(('Name', 'Address_1', 'Address_2', 'Address_3', 'City', 'State', 'Postal Code', 'Country', 'Address Type Code', 'Address PK', 'State FK', 'Country FK', 'Customer FK', 'Last Updated On', 'Deleted B', 'Alert Code', 'FAX Number', 'Contact Name', 'Phone Number', 'Preferred B', 'X Position', 'Y Position', 'TLN Bitmap'))
        self.tableWidgetCode.setColumnWidth(0, 200)
        self.tableWidgetCode.setColumnWidth(1, 150)
        self.tableWidgetCode.setColumnWidth(2, 110)
        self.tableWidgetCode.setColumnWidth(3, 95)
        self.tableWidgetCode.setColumnWidth(4, 95)
        self.tableWidgetCode.setColumnWidth(5, 95)
        self.tableWidgetCode.setColumnWidth(6, 95)
        self.tableWidgetCode.setColumnWidth(7, 95)
        #self.tableWidgetCode.setColumnWidth(8, 95)
        #self.tableWidgetCode.setColumnWidth(9, 95)
        #self.tableWidgetCode.setColumnWidth(10, 95)
        #self.tableWidgetCode.setColumnWidth(11, 95)
        #self.tableWidgetCode.setColumnWidth(12, 95)
        #self.tableWidgetCode.setColumnWidth(13, 95)
        #self.tableWidgetCode.setColumnWidth(14, 95)
        #self.tableWidgetCode.setColumnWidth(15, 95)
        #self.tableWidgetCode.setColumnWidth(16, 95)
        #self.tableWidgetCode.setColumnWidth(17, 95)
        #self.tableWidgetCode.setColumnWidth(18, 95)
        #self.tableWidgetCode.setColumnWidth(19, 95)
        #self.tableWidgetCode.setColumnWidth(20, 95)
        #self.tableWidgetCode.setColumnWidth(21, 95)
        #self.tableWidgetCode.setColumnWidth(22, 95)
        #self.tableWidgetCode.setColumnWidth(23, 95)

    def main():
    #app = QtGui.QApplication(sys.argv) # A new instance of QApplication
    app = QtWidgets.QApplication(sys.argv) # A new instance of QApplication
    form = CodeApp() # We set the form to be our ExampleApp (design) # Show the form
    app.exec_() # and execute the app

    if name == 'main': # if we're running file directly and not importing it
    main() # run the main function

  • @SGaist
    Ok, I think I figured it out using functools. Below is the code that I replaced. Seems to be working but I did have one question, when calling currentItemChanged,connect, I'm passing the row number, it seems as that is the FIRST param sent to handleCombo(), I thought the signal would be the first param sent?

    Just want to make sure I understand this correctly,,,,

    def getData(self):
    #mapper = QSignalMapper()
    rowCount = 5 #len(allSQLRows)

        for row in range(rowCount):
            combo = QtWidgets.QComboBox()
            #mapper.setMapping(combo, index)
            #mapper.setMapping(combo, 1)
            combo.addItem('AAA', 'aaa')
            combo.addItem('BBB', 'bbb')
            combo.addItem('CCC', 'ccc')
            combo.currentIndexChanged.connect(functools.partial(self.handleCombo, row))
    def handleCombo(self, row, index):
            print('Current Row: ' + str(row))
            print('Index of Combo: ' + str(index))

  • Lifetime Qt Champion

    I haven't used functools so I can't comment on that part.

Log in to reply