import PySide2.QtWidgets as QtWidgets app = QtWidgets.QApplication([]) table = QtWidgets.QTableWidget(2, 2) table.setHorizontalHeaderLabels(['Name', 'Age']) item1 = QtWidgets.QTableWidgetItem('John') item2 = QtWidgets.QTableWidgetItem('25') item3 = QtWidgets.QTableWidgetItem('Jane') item4 = QtWidgets.QTableWidgetItem('30') table.setItem(0, 0, item1) table.setItem(0, 1, item2) table.setItem(1, 0, item3) table.setItem(1, 1, item4) table.show() app.exec_()
for row in range(table.rowCount()): name = table.item(row, 0).text() age = table.item(row, 1).text() print(name + ' is ' + age + ' years old.')In this example, a loop is used to iterate over each row in the table. The text of the QTableWidgetItem objects in each column is retrieved using the text() method, and then printed to the console. Package Library: PySide2