title: How to detect the row and column number of cellwidget user clicked on QTableWidget in PyQt4?
body: <p>My objective is to set <code>multiple QPushButton</code> as <code>cellwidget</code> in a <code>QTableWidget</code> and whenever the user click on any of those buttons, it will print out the <code>Row</code> and <code>Column</code> number of table it is in.</p>
  <p>So, for 1 <code>QPushButton</code>,</p>
  <pre><code>import sys
  from PyQt4 import QtGui,QtCore
  
  def CurrentPos():
      clickme = QtGui.qApp.focusWidget()
      index = table.indexAt(clickme.pos())
      if index.isValid():
          print (index.row(), index.column())
          
  def AddValues():
      table.setRowCount(5)
      for i in range(5):
          button = QtGui.QPushButton('Click1')
          table.setCellWidget(i,1,button)
          button.clicked.connect(CurrentPos)
  
  if __name__ == &quot;__main__&quot;:
      app = QtGui.QApplication(sys.argv)
  
      table = QtGui.QTableWidget()
      table.setFixedSize(QtCore.QSize(330,250))
  
      table.setEditTriggers(QtGui.QTableWidget.NoEditTriggers)
      table.setSelectionBehavior(QtGui.QTableWidget.SelectRows)
      table.setSelectionMode(QtGui.QTableWidget.NoSelection)
      table.horizontalHeader().setStretchLastSection(True)
      table.setFocusPolicy(QtCore.Qt.NoFocus)
  
      table.setColumnCount(3)
      table.setHorizontalHeaderLabels(['A','B','C'])
      table.setColumnWidth(0,50)
      table.setColumnWidth(1,200)
      table.show()
  
      AddValues()
      
      app.exec_()
  </code></pre>
  <p><img src="https://i.stack.imgur.com/24FNb.gif" alt="enter image description here" /></p>
  <p>When i click on each button, It will print out the rows and columns number just fine. But When I changed the function <code>AddValues</code> to accomodate multiple buttons:</p>
  <pre><code>def AddValues():
      table.setRowCount(5)
      for i in range(5):
          button1 = QtGui.QPushButton('Click1')
          button2 = QtGui.QPushButton('Click2')
          button3 = QtGui.QPushButton('Click3')
          
          button_layout = QtGui.QHBoxLayout()       
          button_layout.addWidget(button1)
          button_layout.addWidget(button2)
          button_layout.addWidget(button3)
          
          buttons_widget = QtGui.QWidget()
          buttons_widget.setLayout(button_layout)
          
          table.setCellWidget(i,1,buttons_widget)
          button1.clicked.connect(CurrentPos)
          button2.clicked.connect(CurrentPos)
          button3.clicked.connect(CurrentPos)
  </code></pre>
  <p><img src="https://i.stack.imgur.com/3MqGv.gif" alt="enter image description here" /></p>
  <p>It no longer print out the correct row and column number anymore. Instead, It will just print either (0,0) or (0,1) depending on the position of the button.</p>
  <p>I tried to print out the <code>QtGui.qApp.focusWidget()</code> and it gives me <code>&lt;PyQt4.QtGui.QPushButton object at 0x014B56F0&gt;</code> so as far as I understand, the focus widget is the actual <code>QPushButton</code> I am clicking instead of the <code>QWidget</code> named <code>buttons_widget</code>.</p>
  <p>My only suspect is that <code>.pos()</code> method return the position relative to it's parent widget, which is <code>QWidget (buttons_widget)</code> rather than the <code>QTableWidget</code> itself. But I can't seems to find the documentation for <code>.pos()</code> method.</p>
  <p>So, How can I modify my code so that whenever user click on any of 3 Pushbuttons, it print out the row and column number the button currently in.</p>
  <p>I am using windows xp SP3 and Python 2.7.3</p>