{"id":5272,"date":"2022-10-17T02:38:37","date_gmt":"2022-10-17T02:38:37","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=5272"},"modified":"2022-10-17T02:40:00","modified_gmt":"2022-10-17T02:40:00","slug":"pyqt-qtablewidget","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qtablewidget\/","title":{"rendered":"PyQt QTableWidget"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the <code>QTableWidget<\/code> class to create a table widget.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-pyqt-qtablewidget-class'>Introduction to PyQt QTableWidget class <a href=\"#introduction-to-pyqt-qtablewidget-class\" class=\"anchor\" id=\"introduction-to-pyqt-qtablewidget-class\" title=\"Anchor for Introduction to PyQt QTableWidget class\">#<\/a><\/h2>\n\n\n\n<p>The <code><code>QTableWidget<\/code><\/code> class allows you to create a table widget that displays the tabular form of items. The items in the <code><code>QTableWidget<\/code><\/code> are created using the <code><code>QTableWidget<\/code><\/code>Item class.<\/p>\n\n\n\n<p>The following creates a table widget using the <code>QTableWidget<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">table = QTableWidget(parent)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>parent<\/code> is the parent widget or the <a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qmainwindow\/\">main window<\/a>.<\/p>\n\n\n\n<p>Once having a <code>QTableWidget<\/code> object, you can set the number of columns for the table using the <code>setColumnCount()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">table.setColumnCount(columns)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To set the horizontal labels for the table columns, you use the <code>setHorizontalHeaderLabels()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">table.setHorizontalHeaderLabels(labels)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Each column has an index starting from zero. For each column, you can configure its width using the <code>setColumnWidth()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">table.setColumnWidth(column, width)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To set the number of rows for the table, you use the <code>setRowCount()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">table.setRowCount(rows)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you know the number of rows and columns that the table may have at the time of creation, you can use the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">table = QTableWidget(rows, columns, <span class=\"hljs-keyword\">parent<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To add an item to the table, you use the <code>setItem()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">table.setItem(row, column, item)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='pyqt-qtablewidget-example'>PyQt QTableWidget example <a href=\"#pyqt-qtablewidget-example\" class=\"anchor\" id=\"pyqt-qtablewidget-example\" title=\"Anchor for PyQt QTableWidget example\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll develop an application that uses a <code>QTableWidget<\/code> to manage employee data:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"902\" height=\"646\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QTableWidget.png\" alt=\"\" class=\"wp-image-5274\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QTableWidget.png 902w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QTableWidget-300x215.png 300w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QTableWidget-768x550.png 768w\" sizes=\"auto, (max-width: 902px) 100vw, 902px\" \/><\/figure>\n\n\n\n<p>If you enter the first name, last name, and age and click add, the program will add the new employee to the table.  Also, if you select a row and click the delete icon, the table will delete the row.<\/p>\n\n\n\n<p>Here&#8217;s the complete program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> sys\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtWidgets <span class=\"hljs-keyword\">import<\/span> (\n    QApplication, QMainWindow, QTableWidget, \n    QTableWidgetItem, QDockWidget, QFormLayout, \n    QLineEdit, QWidget, QPushButton, QSpinBox, \n    QMessageBox, QToolBar, QMessageBox\n)\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtCore <span class=\"hljs-keyword\">import<\/span> Qt,QSize\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtGui <span class=\"hljs-keyword\">import<\/span> QIcon, QAction\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MainWindow<\/span><span class=\"hljs-params\">(QMainWindow)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, *args, **kwargs)<\/span>:<\/span>\n        super().__init__(*args, **kwargs)\n        self.setWindowTitle(<span class=\"hljs-string\">'Employees'<\/span>)\n        self.setWindowIcon(QIcon(<span class=\"hljs-string\">'.\/assets\/usergroup.png'<\/span>))\n        self.setGeometry(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">600<\/span>, <span class=\"hljs-number\">400<\/span>)\n\n        employees = &#91;\n            {<span class=\"hljs-string\">'First Name'<\/span>: <span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-string\">'Last Name'<\/span>: <span class=\"hljs-string\">'Doe'<\/span>, <span class=\"hljs-string\">'Age'<\/span>: <span class=\"hljs-number\">25<\/span>},\n            {<span class=\"hljs-string\">'First Name'<\/span>: <span class=\"hljs-string\">'Jane'<\/span>, <span class=\"hljs-string\">'Last Name'<\/span>: <span class=\"hljs-string\">'Doe'<\/span>, <span class=\"hljs-string\">'Age'<\/span>: <span class=\"hljs-number\">22<\/span>},\n            {<span class=\"hljs-string\">'First Name'<\/span>: <span class=\"hljs-string\">'Alice'<\/span>, <span class=\"hljs-string\">'Last Name'<\/span>: <span class=\"hljs-string\">'Doe'<\/span>, <span class=\"hljs-string\">'Age'<\/span>: <span class=\"hljs-number\">22<\/span>},\n        ]\n\n        self.table = QTableWidget(self)\n        self.setCentralWidget(self.table)\n\n        self.table.setColumnCount(<span class=\"hljs-number\">3<\/span>)\n        self.table.setColumnWidth(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">150<\/span>)\n        self.table.setColumnWidth(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">150<\/span>)\n        self.table.setColumnWidth(<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">50<\/span>)\n\n        self.table.setHorizontalHeaderLabels(employees&#91;<span class=\"hljs-number\">0<\/span>].keys())\n        self.table.setRowCount(len(employees))\n\n        row = <span class=\"hljs-number\">0<\/span>\n        <span class=\"hljs-keyword\">for<\/span> e <span class=\"hljs-keyword\">in<\/span> employees:\n            self.table.setItem(row, <span class=\"hljs-number\">0<\/span>, QTableWidgetItem(e&#91;<span class=\"hljs-string\">'First Name'<\/span>]))\n            self.table.setItem(row, <span class=\"hljs-number\">1<\/span>, QTableWidgetItem(e&#91;<span class=\"hljs-string\">'Last Name'<\/span>]))\n            self.table.setItem(row, <span class=\"hljs-number\">2<\/span>, QTableWidgetItem(str(e&#91;<span class=\"hljs-string\">'Age'<\/span>])))\n            row += <span class=\"hljs-number\">1<\/span>\n\n        dock = QDockWidget(<span class=\"hljs-string\">'New Employee'<\/span>)\n        dock.setFeatures(QDockWidget.DockWidgetFeature.NoDockWidgetFeatures)\n        self.addDockWidget(Qt.DockWidgetArea.RightDockWidgetArea, dock)\n\n        <span class=\"hljs-comment\"># create form<\/span>\n        form = QWidget()\n        layout = QFormLayout(form)\n        form.setLayout(layout)\n\n\n        self.first_name = QLineEdit(form)\n        self.last_name = QLineEdit(form)\n        self.age = QSpinBox(form, minimum=<span class=\"hljs-number\">18<\/span>, maximum=<span class=\"hljs-number\">67<\/span>)\n        self.age.clear()\n\n        layout.addRow(<span class=\"hljs-string\">'First Name:'<\/span>, self.first_name)\n        layout.addRow(<span class=\"hljs-string\">'Last Name:'<\/span>, self.last_name)\n        layout.addRow(<span class=\"hljs-string\">'Age:'<\/span>, self.age)\n\n        btn_add = QPushButton(<span class=\"hljs-string\">'Add'<\/span>)\n        btn_add.clicked.connect(self.add_employee)\n        layout.addRow(btn_add)\n\n        <span class=\"hljs-comment\"># add delete &amp; edit button<\/span>\n        toolbar = QToolBar(<span class=\"hljs-string\">'main toolbar'<\/span>)\n        toolbar.setIconSize(QSize(<span class=\"hljs-number\">16<\/span>,<span class=\"hljs-number\">16<\/span>))\n        self.addToolBar(toolbar)\n\n\n        delete_action = QAction(QIcon(<span class=\"hljs-string\">'.\/assets\/remove.png'<\/span>), <span class=\"hljs-string\">'&amp;Delete'<\/span>, self)\n        delete_action.triggered.connect(self.delete)\n        toolbar.addAction(delete_action)\n        dock.setWidget(form)\n\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">delete<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        current_row = self.table.currentRow()\n        <span class=\"hljs-keyword\">if<\/span> current_row &lt; <span class=\"hljs-number\">0<\/span>:\n            <span class=\"hljs-keyword\">return<\/span> QMessageBox.warning(self, <span class=\"hljs-string\">'Warning'<\/span>,<span class=\"hljs-string\">'Please select a record to delete'<\/span>)\n\n        button = QMessageBox.question(\n            self,\n            <span class=\"hljs-string\">'Confirmation'<\/span>,\n            <span class=\"hljs-string\">'Are you sure that you want to delete the selected row?'<\/span>,\n            QMessageBox.StandardButton.Yes |\n            QMessageBox.StandardButton.No\n        )\n        <span class=\"hljs-keyword\">if<\/span> button == QMessageBox.StandardButton.Yes:\n            self.table.removeRow(current_row)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">valid<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        first_name = self.first_name.text().strip()\n        last_name = self.last_name.text().strip()\n\n        \n        <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-keyword\">not<\/span> first_name:\n            QMessageBox.critical(self, <span class=\"hljs-string\">'Error'<\/span>, <span class=\"hljs-string\">'Please enter the first name'<\/span>)\n            self.first_name.setFocus()\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">False<\/span>\n\n        <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-keyword\">not<\/span> last_name:\n            QMessageBox.critical(self, <span class=\"hljs-string\">'Error'<\/span>, <span class=\"hljs-string\">'Please enter the last name'<\/span>)\n            self.last_name.setFocus()\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">False<\/span>\n\n        <span class=\"hljs-keyword\">try<\/span>:\n            age = int(self.age.text().strip())\n        <span class=\"hljs-keyword\">except<\/span> ValueError:\n            QMessageBox.critical(self, <span class=\"hljs-string\">'Error'<\/span>, <span class=\"hljs-string\">'Please enter a valid age'<\/span>)\n            self.age.setFocus()\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">False<\/span>\n\n        <span class=\"hljs-keyword\">if<\/span> age &lt;= <span class=\"hljs-number\">0<\/span> <span class=\"hljs-keyword\">or<\/span> age &gt;= <span class=\"hljs-number\">67<\/span>:\n            QMessageBox.critical(\n                self, <span class=\"hljs-string\">'Error'<\/span>, <span class=\"hljs-string\">'The valid age is between 1 and 67'<\/span>)\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">False<\/span>\n\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">True<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">reset<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.first_name.clear()\n        self.last_name.clear()\n        self.age.clear()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">add_employee<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-keyword\">not<\/span> self.valid():\n            <span class=\"hljs-keyword\">return<\/span>\n\n        row = self.table.rowCount()\n        self.table.insertRow(row)\n        self.table.setItem(row, <span class=\"hljs-number\">0<\/span>, QTableWidgetItem(\n            self.first_name.text().strip())\n        )\n        self.table.setItem(\n            row, <span class=\"hljs-number\">1<\/span>, QTableWidgetItem(self.last_name.text())\n        )\n        self.table.setItem(\n            row, <span class=\"hljs-number\">2<\/span>, QTableWidgetItem(self.age.text())\n        )\n\n        self.reset()\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    app = QApplication(sys.argv)\n    window = MainWindow()\n    window.show()\n    sys.exit(app.exec())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, initialize an employee list as a list of dictionaries:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">employees = &#91;\n    {<span class=\"hljs-string\">'First Name'<\/span>: <span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-string\">'Last Name'<\/span>: <span class=\"hljs-string\">'Doe'<\/span>, <span class=\"hljs-string\">'Age'<\/span>: <span class=\"hljs-number\">25<\/span>},\n    {<span class=\"hljs-string\">'First Name'<\/span>: <span class=\"hljs-string\">'Jane'<\/span>, <span class=\"hljs-string\">'Last Name'<\/span>: <span class=\"hljs-string\">'Doe'<\/span>, <span class=\"hljs-string\">'Age'<\/span>: <span class=\"hljs-number\">22<\/span>},\n    {<span class=\"hljs-string\">'First Name'<\/span>: <span class=\"hljs-string\">'Alice'<\/span>, <span class=\"hljs-string\">'Last Name'<\/span>: <span class=\"hljs-string\">'Doe'<\/span>, <span class=\"hljs-string\">'Age'<\/span>: <span class=\"hljs-number\">22<\/span>},\n]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, create a new <code>QTableWidget<\/code> and set it as the central widget of the main window:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.table = QTableWidget(self)\nself.setCentralWidget(self.table)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, set the number of columns for the table and configure the columns&#8217; width:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.table.setColumnCount(<span class=\"hljs-number\">3<\/span>)\nself.table.setColumnWidth(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">150<\/span>)\nself.table.setColumnWidth(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">150<\/span>)\nself.table.setColumnWidth(<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">50<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, set the horizontal headers for the table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.table.setHorizontalHeaderLabels(employees&#91;<span class=\"hljs-number\">0<\/span>].keys())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fifth, set the row count as the number of items in the <code>employees<\/code> list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.table.setRowCount(len(employees))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Sixth, add each employee from the employees to the table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">row = <span class=\"hljs-number\">0<\/span>\n<span class=\"hljs-keyword\">for<\/span> e <span class=\"hljs-keyword\">in<\/span> employees:\n    self.table.setItem(row, <span class=\"hljs-number\">0<\/span>, QTableWidgetItem(e&#91;<span class=\"hljs-string\">'First Name'<\/span>]))\n    self.table.setItem(row, <span class=\"hljs-number\">1<\/span>, QTableWidgetItem(e&#91;<span class=\"hljs-string\">'Last Name'<\/span>]))\n    self.table.setItem(row, <span class=\"hljs-number\">2<\/span>, QTableWidgetItem(str(e&#91;<span class=\"hljs-string\">'Age'<\/span>])))\n    row += <span class=\"hljs-number\">1<\/span>   <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Seventh, define the <code>delete()<\/code> method that executes when the user selects a row and clicks the delete button on the <a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qtoolbar\/\">toolbar<\/a>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">delete<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n    current_row = self.table.currentRow()\n    <span class=\"hljs-keyword\">if<\/span> current_row &lt; <span class=\"hljs-number\">0<\/span>:\n        <span class=\"hljs-keyword\">return<\/span> QMessageBox.warning(self, <span class=\"hljs-string\">'Warning'<\/span>,<span class=\"hljs-string\">'Please select a record to delete'<\/span>)\n\n    button = QMessageBox.question(\n        self,\n        <span class=\"hljs-string\">'Confirmation'<\/span>,\n        <span class=\"hljs-string\">'Are you sure that you want to delete the selected row?'<\/span>,\n        QMessageBox.StandardButton.Yes |\n        QMessageBox.StandardButton.No\n    )\n    <span class=\"hljs-keyword\">if<\/span> button == QMessageBox.StandardButton.Yes:\n        self.table.removeRow(current_row)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>currenRow()<\/code> method returns the currently selected row. It returns -1 if no row is selected. In this case, we use the <code>QMessageBox<\/code> to issue a warning message.<\/p>\n\n\n\n<p>If the user selects a row, we use the <code>QMessageBox<\/code> to raise a question to confirm the deletion. If the user clicks <code>OK<\/code>, we use the <code>removeRow()<\/code> method of the <code>QTableWidget<\/code> to delete the selected row.<\/p>\n\n\n\n<p>Eight, define the <code>add_employee()<\/code> method to add a new employee to the table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">add_employee<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-keyword\">not<\/span> self.valid():\n        <span class=\"hljs-keyword\">return<\/span>\n    row = self.table.rowCount()\n    self.table.insertRow(row)\n    self.table.setItem(row, <span class=\"hljs-number\">0<\/span>, QTableWidgetItem(self.first_name.text().strip()))\n    self.table.setItem(row, <span class=\"hljs-number\">1<\/span>, QTableWidgetItem(self.last_name.text()))\n    self.table.setItem(row, <span class=\"hljs-number\">2<\/span>, QTableWidgetItem(self.age.text()))\n    self.reset()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>add_employee()<\/code> method does the following: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Validate the employee using the <code>valid()<\/code> method. <\/li><li>Append a new row to the table using the <code>insertRow()<\/code> method. <\/li><li>Set the item for each column of the newly inserted row. using the <code>setItem()<\/code> method.<\/li><li>Reset the employee form using the <code>reset()<\/code> method.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>QTableWidget<\/code> class to create a table widget.<\/li><li>Use the <code>setColumnCount()<\/code> and <code>setRowCount()<\/code> methods to set the columns and rows for the table.<\/li><li>Use the <code>setHorizontalHeaderLabels()<\/code> method to set the horizontal headers for the table.<\/li><li>Use the <code>QTableWidgetItem<\/code> class to create a table item.<\/li><li>Use the <code>setItem()<\/code> method to set an item for the table.<\/li><li>Use the <code>currentRow()<\/code> method to get the currently selected row.<\/li><li>Use the <code>insertRow()<\/code> method to insert a new row into the table.<\/li><li>Use the <code>deleteRow()<\/code> method to delete a row from the table.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"5272\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qtablewidget\/\"\n\t\t\t\tdata-post-title=\"PyQt QTableWidget\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"5272\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qtablewidget\/\"\n\t\t\t\tdata-post-title=\"PyQt QTableWidget\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the QTableWidget class to create a table widget.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4862,"menu_order":31,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5272","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=5272"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5272\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4862"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=5272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}