Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect the row and column number of cellwidget user clicked on QTableWidget in PyQt4?

My objective is to set multiple QPushButton as cellwidget in a QTableWidget and whenever the user click on any of those buttons, it will print out the Row and Column number of table it is in.

So, for 1 QPushButton,

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__ == "__main__":
    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_()

enter image description here

When i click on each button, It will print out the rows and columns number just fine. But When I changed the function AddValues to accomodate multiple buttons:

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)

enter image description here

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.

I tried to print out the QtGui.qApp.focusWidget() and it gives me <PyQt4.QtGui.QPushButton object at 0x014B56F0> so as far as I understand, the focus widget is the actual QPushButton I am clicking instead of the QWidget named buttons_widget.

My only suspect is that .pos() method return the position relative to it's parent widget, which is QWidget (buttons_widget) rather than the QTableWidget itself. But I can't seems to find the documentation for .pos() method.

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.

I am using windows xp SP3 and Python 2.7.3

like image 833
Chris Aung Avatar asked Dec 08 '22 08:12

Chris Aung


1 Answers

I found the solution.

index = table.indexAt(clickme.parent().pos())

I should be checking the position of the parent QWidget rather than the QPushButton

like image 97
Chris Aung Avatar answered Dec 28 '22 06:12

Chris Aung