Search code examples
pythonpyqtpyqt4

Select a table cell via python and PyQt4


I have the code below and I just want to find the command that selects a specific cell and NOT a column or a row.

for example:

QTableWidget().item(2,0)

the code is :

def main():
    app = QApplication(sys.argv)
    table = QTableWidget()
    tableItem = QTableWidgetItem()

    # initiate table
    table.setWindowTitle("QTableWidget Example @pythonspot.com")
    table.resize(400, 250)
    table.setRowCount(4)
    table.setVerticalHeaderLabels(["Number 0ne","Number Two","Number Three","Number Four"])
    table.setColumnCount(2)
    table.setHorizontalHeaderLabels(['Name', 'Age'])


    for x in range(4):
        for y in range(2):
             table.setItem(x, y, QTableWidgetItem("Item (%d ,  %d)"% ((x+1),(y+1))))


    # show table
    table.show()
    app.exec_()


if __name__ == '__main__':
    main()

and I wanna know what is the best reference for PyQt commands which helps the coder about the command's arguments?

for example, where is the best place to inform about what can be the QString in this command: setAccessibleName(self, QString)


Solution

  • You could use the item method, table.item(2,0) for example. Using it in your code would return <PyQt5.QtWidgets.QTableWidgetItem object at 0x7f5154057af8>, which is the object handle for the widget located in the (2,0) table's cell.

    The problem with the code you mentioned (QTableWidget().item(2,0)) is that you need to call it from the instance of the QTableWidget class, i.e. table in your case.

    You could use either the PyQt docs or the PySide docs, which are very similar to the PyQt (most of the commands are exactly the same, but caution is advised) but have better readability.

    PySide Docs for QTableWidget for example.