Use of Pyqt's QTableWidget component
QTableWidget
is a table control in PyQt6 for displaying and editing two-dimensional tabular data. It inherits fromQTableView
, provides an easier way to process tabular data, suitable for scenarios where structured data is required.
1. Common methods
1.1 Constructor
QTableWidget(parent: QWidget = None)
: Create an empty table control.QTableWidget(rows: int, columns: int, parent: QWidget = None)
: Create a table control that specifies the number of rows and columns.
1.2 Set the number of rows and columns
setRowCount(rows: int)
: Set the number of rows in the table.setColumnCount(columns: int)
: Set the number of columns in the table.rowCount() -> int
: Get the number of rows in the table.columnCount() -> int
: Get the number of columns in the table.
1.3 Setting the table header
setHorizontalHeaderLabels(labels: List[str])
: Set the label of the horizontal table header.setVerticalHeaderLabels(labels: List[str])
: Set the label of the vertical header.horizontalHeader() -> QHeaderView
: Get the horizontal table header object.verticalHeader() -> QHeaderView
: Get the vertical header object.
1.4 Set cell content
setItem(row: int, column: int, item: QTableWidgetItem)
: Set the contents of the specified cell.item(row: int, column: int) -> QTableWidgetItem
: Get the contents of the specified cell.takeItem(row: int, column: int) -> QTableWidgetItem
: Removes the contents of the specified cell and returns.
1.5 Set cell properties
setCellWidget(row: int, column: int, widget: QWidget)
: Place a control (such as buttons, drop-down boxes, etc.) in the specified cell.cellWidget(row: int, column: int) -> QWidget
: Gets the control in the specified cell.setSpan(row: int, column: int, rowSpan: int, columnSpan: int)
: Merge cells.
1.6 Select Mode
setSelectionMode(mode: )
: Set selection mode (such as single-select, multiple-select, etc.).setSelectionBehavior(behavior: )
: Set selection behavior (such as selecting rows, selecting columns, etc.).
1.7 Signal
cellChanged(row: int, column: int)
: Triggered when the cell content changes.cellClicked(row: int, column: int)
: Triggered when the cell is clicked.cellDoubleClicked(row: int, column: int)
: Fired when the cell is double-clicked.
2. Example
import os import sys import webbrowser import qt_material from import * from import * class Window(QWidget): def __init__(self): super(Window, self).__init__() (400, 300) self.init_widget() self.init_signal() self.init_getdata() self.init_layout() def init_widget(self): # tablewidget = QTableWidget() (2) (['website', 'Url']) ().setSectionResizeMode() (True) () # Cannot edit def init_getdata(self): row_count = () (row_count + 1) (0, 0, QTableWidgetItem('Baidu')) (0, 1, QTableWidgetItem('/')) row_count = () (row_count + 1) (1, 0, QTableWidgetItem('Tencent Video')) (1, 1, QTableWidgetItem('/')) def init_signal(self): (self.open_url) # Double-click to open the web page def init_layout(self): v_layout = QVBoxLayout() v_layout.addWidget() (v_layout) def open_url(self, row, column): print(f'{row} {column}') if int(column) == 1: url = (row, column).text() (url) if __name__ == '__main__': ["QT_IM_MODULE"] = "ibus" # mac system solves incompatible input method app = QApplication() qt_material.apply_stylesheet(app, theme='light_pink.xml') window = Window() () (())
3. Summary
advantage
Simple and easy to use: Provides an interface for directly manipulating table data.
High flexibility: Supports dynamic loading of data, editing cell content and embedding controls.
Rich features: Supports functions such as header setting, cell merging, and selection mode.
Applicable scenarios
Data display (such as grade tables, inventory tables).
Data editing (such as employee information management, order management).
Dynamic data loading (such as database query results, CSV file data).
This is the end of this article about the use of Pyqt's QTableWidget component. For more information about Pyqt QTableWidget component, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!
Related Articles
Python timer, implement the method of execution at 3 am every day
Today, the editor will share with you a python timer, which implements the method of executing at 3 a.m. every day. It has good reference value and hopes it will be helpful to everyone. Let's take a look with the editor2019-02-02Python speech synthesis third-party library gTTs/pyttsx3/speech horizontal review (with usage method)
Python is a very powerful scripting language that can be used to implement various complex applications. One of them is text to voice, that is, converting text into sound to send out. The following article mainly introduces relevant information about the third-party library gTTs/pyttsx3/speech horizontal comments for Python speech synthesis. The article also introduces detailed usage methods. Friends who need it can refer to it.2023-05-05Complete explanation of the implementation process of Python carousel diagram and navigation bar function
This article mainly introduces the implementation of the Python project carousel diagram function and the implementation of the navigation bar. The example code is introduced in this article in detail, which has certain reference learning value for everyone's learning or work. Friends who need it, please learn with the editor below.2022-09-09Python command line timing task automation workflow
This article describes how to write timed tasks in Python to automatically execute command-line tasks. You will learn how to schedule regular tasks, process task results, and how to automate workflows using Python to improve productivity. No need to manually execute repetitive tasks, Python will help you solve them2023-04-04Python interactive graphical programming example (II)
This article mainly introduces python interactive graphics programming examples in detail, which have certain reference value. Interested friends can refer to it.2017-11-11python tuple operation instance analysis
This article mainly introduces python tuple operations, and explains the characteristics and usage of tuples in Python in the form of examples. Friends who need it can refer to it2014-09-09Python uses xmltodict to implement example code to convert dictionary and xml to each other
xmltodict is a third-party Python library used to process XML data. The article introduces the sample code in detail and has a certain reference learning value for everyone's learning or work. If you need it, please learn with the editor below.2024-12-12Methods for python scripts to monitor Tomcat server
This article mainly introduces the method of using python scripts to monitor Tomcat servers. This article introduces you very detailed and has certain reference value. Friends who need it can refer to it.2018-07-07Efficient output console logs and logging in Python loguru log library
This article mainly introduces the relevant knowledge of efficient output console logs and log records in the Python loguru log library. This article introduces you very detailedly and has certain reference value for your study or work. Friends who need it can refer to it.2020-03-03Detailed explanation of merge and splitting arrays in numpy and pandas
Below, the editor will share with you a detailed explanation of the merge and splitting of arrays in numpy and pandas. It has good reference value and hope it will be helpful to everyone. Let's take a look with the editor2018-04-04