Day 25 | day 28 learn PyQt5, QTableWidget of advanced controls

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:



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;


Set the alignment of the text in the cell;


Get the subscript of the selected column;


Obtain the subscript of the selected row;


After getting the item, you can use the text() method to get the content;


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:

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):

    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
        # Input content
        for (row, customer) in enumerate(self.customer_list):
            for column in range(len(customer)):
                self.table_widget.setItem(row, column,
        # Merge rows and columns
        self.table_widget.setSpan(0, 1, 2, 1)
        # Cells are not editable
        # Select single line
        # Click event
        # Double click the event
        # resize window
        self.resize(900, 500)
        # Center window
        # Window title
        self.setWindowTitle("QTableWidget application")
        # Display window
        # 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:
        # Select row
        row = self.table_widget.currentRow()
        # Selected column
        column = self.table_widget.currentColumn()
        data = self.table_widget.item(row, column).text()

    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()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = TableWidget()

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

Keywords: Python Data Analysis PyQt5

Added by dbrown on Mon, 04 Oct 2021 05:57:34 +0300