from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem app = QApplication([]) table = QTableWidget() table.setRowCount(2) table.setColumnCount(2) table.setItem(0, 0, QTableWidgetItem()) table.setItem(0, 1, QTableWidgetItem()) table.setItem(1, 0, QTableWidgetItem()) table.setItem(1, 1, QTableWidgetItem()) table.item(0, 0).setText("Hello") table.item(0, 1).setText("World") table.item(1, 0).setText("Python") table.item(1, 1).setText("Qt") table.show() app.exec_()
def add_row(table, name, age): row_position = table.rowCount() table.insertRow(row_position) table.setItem(row_position, 0, QTableWidgetItem(name)) table.setItem(row_position, 1, QTableWidgetItem(str(age))) app = QApplication([]) table = QTableWidget() table.setColumnCount(2) table.setHorizontalHeaderLabels(['Name', 'Age']) add_row(table, "John", 25) add_row(table, "Mary", 30) add_row(table, "Jane", 27) table.show() app.exec_()In this example, we have a function that adds a row to a QTableWidget object. We use the setText() function to set the text of the QTableWidgetItem objects for the name and age columns.