pyside2 qtablewidget
时间: 2023-09-21 13:11:20 浏览: 147
【Pyside资源】基于openpyxl和PySide2模块实现的Excel操作通用类
PySide2 QTableWidget is a GUI component in the PySide2 library that provides a table view widget for displaying and editing tabular data. It is similar to a spreadsheet or a database table, where the data is arranged in rows and columns.
To use QTableWidget, you need to import the module and create an instance of the QTableWidget class. You can set the number of rows and columns, and populate the cells with data using the setItem() method.
Here's an example code snippet that creates a QTableWidget with two columns and three rows:
```python
from PySide2.QtWidgets import QApplication, QTableWidget, QTableWidgetItem
app = QApplication([])
table = QTableWidget(3, 2) # 3 rows, 2 columns
# Set the headers for the columns
table.setHorizontalHeaderLabels(['Name', 'Age'])
# Populate the cells with data
table.setItem(0, 0, QTableWidgetItem('Alice'))
table.setItem(0, 1, QTableWidgetItem('25'))
table.setItem(1, 0, QTableWidgetItem('Bob'))
table.setItem(1, 1, QTableWidgetItem('30'))
table.setItem(2, 0, QTableWidgetItem('Charlie'))
table.setItem(2, 1, QTableWidgetItem('35'))
table.show()
app.exec_()
```
This code creates a table with two columns: "Name" and "Age", and populates the cells with data for three people. The `show()` method displays the table, and `exec_()` starts the application event loop.
You can also customize the appearance and behavior of the QTableWidget by setting properties and connecting signals and slots. For example, you can enable editing of cells, set the selection mode, and handle cell clicks and edits.
阅读全文