Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. currentRow() in QTableWidget is returning wrong row # when user clicks on a QComboBox

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

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 3 Posters 6.9k Views
  • 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.
  • SGaistS SGaist

    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.

    G Offline
    G Offline
    Goldglv
    wrote on last edited by
    #10

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

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

      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
          self.tableWidgetCode.setCellWidget(index,0,combo)
          mapper.setMapping(combo, index)
          combo.currentIndexChanged.connect(mapper.map)
          mapper.setMapping(combo, 1)
      
      mapper.mapped.connect(self.handleCombo)
      
      def handleCombo(self, index):
          //update the model with the content of the combo box
      

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

      G 1 Reply Last reply
      0
      • SGaistS SGaist

        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
            self.tableWidgetCode.setCellWidget(index,0,combo)
            mapper.setMapping(combo, index)
            combo.currentIndexChanged.connect(mapper.map)
            mapper.setMapping(combo, 1)
        
        mapper.mapped.connect(self.handleCombo)
        
        def handleCombo(self, index):
            //update the model with the content of the combo box
        
        G Offline
        G Offline
        Goldglv
        wrote on last edited by
        #12

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

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

          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 ?

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

          G 1 Reply Last reply
          0
          • SGaistS SGaist

            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 ?

            G Offline
            G Offline
            Goldglv
            wrote on last edited by Goldglv
            #14

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

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

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

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

              G 1 Reply Last reply
              0
              • SGaistS SGaist

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

                G Offline
                G Offline
                Goldglv
                wrote on last edited by
                #16

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

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

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

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

                  G 2 Replies Last reply
                  0
                  • SGaistS SGaist

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

                    G Offline
                    G Offline
                    Goldglv
                    wrote on last edited by
                    #18

                    @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):
                    #PyQt5.QtWidgets.QMainWindow
                    def init(self):
                    super(self.class, self).init()
                    self.setupUi(self) # This is defined in editor.py file automatically
                    self.pushButtonClose.clicked.connect(self.closeWindow)
                    #mapper = pyqtSignal()
                    self.setColumnHeadings()

                        self.getData()
                        #mapper = QSignalMapper()
                    
                    def getData(self):
                        mapper = QSignalMapper()
                        rowCount = 5 #len(allSQLRows)
                        self.tableWidgetCode.setRowCount(5)
                    
                        for index in range(rowCount):
                            combo = QtWidgets.QComboBox()
                            #self.tableWidgetCode.setCellWidget(index,0,combo)
                            mapper.setMapping(combo, index)
                            combo.currentIndexChanged.connect(mapper.map)
                            mapper.setMapping(combo, 1)
                    
                            mapper.mapped.connect(self.handleCombo)
                        
                            combo.addItem('AAA', 'aaa')
                            combo.addItem('BBB', 'bbb')
                            combo.addItem('CCC', 'ccc')
                            #combo.currentIndexChanged.connect(self.handleCombo)
                            #print('Index: ' + str(index))    
                            self.tableWidgetCode.setCellWidget(index,0,combo) 
                            
                            
                    
                    
                    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):
                        self.close()
                    
                    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)"]
                        
                        #Code
                        #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.horizontalHeader().setStretchLastSection(True)
                        #self.tableWidgetCode.horizontalHeader().setSectionResizeMode(QHeaderView.Fixed)
                        self.tableWidgetCode.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive)
                        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)
                    form.show() # 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

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

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

                      G Offline
                      G Offline
                      Goldglv
                      wrote on last edited by
                      #19

                      @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)
                      self.tableWidgetCode.setRowCount(5)

                          for row in range(rowCount):
                              combo = QtWidgets.QComboBox()
                              ###self.tableWidgetCode.setCellWidget(index,0,combo)
                              #mapper.setMapping(combo, index)
                              #combo.currentIndexChanged.connect(mapper.map)
                              #mapper.setMapping(combo, 1)
                              #mapper.mapped.connect(self.handleCombo)
                              
                              combo.addItem('AAA', 'aaa')
                              combo.addItem('BBB', 'bbb')
                              combo.addItem('CCC', 'ccc')
                              self.tableWidgetCode.setCellWidget(row,0,combo) 
                              combo.currentIndexChanged.connect(functools.partial(self.handleCombo, row))
                      
                      def handleCombo(self, row, index):
                              print('Current Row: ' + str(row))
                              print('Index of Combo: ' + str(index))
                      
                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #20

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

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

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved