The table control also has a QTableWidget, which inherits from QTableView. The main difference between QTableWidget and QTableView is that QTableView can use custom data model to display content, while QTableWidget can only use standard data model, and its cell data is realized through qtablewidgettitem object. In general, using QTableWidget can meet most of our requirements. In addition to the QTableView method, it can also use the methods shown in the following table:
method | describe |
setRowCount(int rows) | Set the number of rows of the table control; |
setColumnCount(int column) | Set the number of columns of the table control; |
setHorizontalHeaderLabels([str] labels) | Used to set the text displayed in multiple sections of the horizontal header in sequence at one time. This method has no return value. A section with a label set will automatically create the item corresponding to the section; |
setVerticalHeaderLabels([str] labels) | Used to set the text displayed in multiple sections of the vertical header in sequence at one time. This method has no return value. A section with a label set will automatically create the item corresponding to the section; |
setTextAlignment() | Set the alignment of the text in the cell; |
currentColumn() | Get the subscript of the selected column; |
currentRow() | Obtain the subscript of the selected row; |
currentItem() | After getting the item, you can use the text() method to get the content; |
selectedItems() | Get the list [qtablewidgettitem], and you can cycle to get the content value of each column; |
item(int row, int column) | Get content with the subscripts of rows and columns. |
Program list: tablewidget.py
import sys from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget, QHBoxLayout, QTableWidget, QTableWidgetItem, QAbstractItemView # Inherit QWidget class TableWidget(QWidget): customer_list = [("Zhang San", "male", "1981-06-02", "13888888888", "No. 999, No. 9 near Penguin House, Antarctica Road"), ("Li Si", "male", "1988-08-08", "13999999999", "666, No. 6, downhill road, polar bear store"), ("Li Qingzhao", "female", "1986-06-06", "13666666666", "888 Road, No. 8, Miaowan Road, ancient poetry, Qinling")] table_widget = None def __init__(self): super().__init__() self.init_ui() def init_ui(self): # Set layout layout = QHBoxLayout() # Data hierarchy, 10 rows and 5 columns self.table_widget = QTableWidget(10, 5) # Last column auto stretch self.table_widget.horizontalHeader().setStretchLastSection(True) # Input content for (row, customer) in enumerate(self.customer_list): for column in range(len(customer)): self.table_widget.setItem(row, column, QTableWidgetItem(customer[column])) layout.addWidget(self.table_widget) # Merge rows and columns self.table_widget.setSpan(0, 1, 2, 1) # Cells are not editable self.table_widget.setEditTriggers(QTableWidget.NoEditTriggers) # Select single line self.table_widget.setSelectionBehavior(QAbstractItemView .SelectRows) # Click event self.table_widget.cellClicked.connect(self.table_click) # Double click the event self.table_widget.cellDoubleClicked.connect(self.double_click) self.setLayout(layout) # resize window self.resize(900, 500) # Center window self.center() # Window title self.setWindowTitle("QTableWidget application") # Display window self.show() # Get folder path def table_click(self): # Gets the column of the selected text item_list = self.table_widget.selectedItems() for item in item_list: print(item.text()) # Select row row = self.table_widget.currentRow() # Selected column column = self.table_widget.currentColumn() data = self.table_widget.item(row, column).text() print(data) def double_click(self): # Select row data = self.table_widget.currentItem() print("Data of selected cells:%s" % data.text()) # Achieve centering def center(self): f = self.frameGeometry() c = QDesktopWidget().availableGeometry().center() f.moveCenter(c) self.move(f.topLeft()) if __name__ == "__main__": app = QApplication(sys.argv) w = TableWidget() sys.exit(app.exec_())
After running the program, the pop-up window is as follows:
Well, that's all for QTableWidget. Pay attention to me and the next section will be more exciting.
Today's headline: Lao Chen said that Python will be fully shared by 2021 national day. The complete courses include:
1. 12 days
2. Data analysis in 16 days
3. Python web crawler in 10 days
four Django3.0 project practice
five Wxpython in 25 days
six The 28 day society PyQt5 is being released
seven The 25 day society Seaborn data analysis was released on csdn
eight "3-day Pyecharts data analysis" was released during the national day