QItemSelection
It is a class used in the PyQt5 model/view framework to manage multiple project selection ranges, which are often used to handle the user's selected status in multiple selection operations (such as multiple selections for tables and lists). It stores multipleQItemSelectionRange
Objects to record complex selection areas and support selection across rows, columns, or any cell combination.
Core functions
- Multi-range selection: Supports storing multiple independent selection areas (such as non-sequentially selected rows or cells).
- Index operation: Merge, split or query selection ranges.
- Interact with the model:
QItemSelectionModel
Cooperate to manage the selected status of the view component.
Common methods
method | illustrate | Return value |
---|---|---|
append(range: QItemSelectionRange) | Add a selection range to the currently selected area | None |
merge(other: QItemSelection) | Merge another QItemSelection to the current instance | None |
split(range: QItemSelectionRange) | Split the specified range into smaller ranges | QItemSelection |
contains(index: QModelIndex) | Determine whether an index is within the selected range | bool |
count() | Returns the number of selected ranges | int |
clear() | Clear all selection ranges | None |
Key Code Examples
1. Create and operate selection ranges
from import QItemSelection, QItemSelectionRange, QModelIndex # Assume that the model indexes index1 and index2 exist (such as cells in a table)index1 = QModelIndex() # When using it, you need to obtain a valid index from the modelindex2 = QModelIndex() # Example: (0, 0), (2, 2) # Create a selection range (rectangular area from index1 to index2)range1 = QItemSelectionRange(index1, index2) # Initialize QItemSelection and add rangeselection = QItemSelection() (range1) # Check whether the index is selectedprint("Include index1:", (index1)) # True
2. Merge two selection areas
# Create another selection rangerange2 = QItemSelectionRange((3, 0), (3, 2)) selection2 = QItemSelection() (range2) # Merge into the first selection area(selection2) print("Number of scopes after merge:", ()) # 2
Use with QItemSelectionModel
QItemSelectionModel
is the management view (such asQTableView
) The core class of the state is selected. Pass signalselectionChanged
Capable the user's selected action.
from import QTableView, QApplication from import QItemSelectionModel class MyTableView(QTableView): def __init__(self): super().__init__() # Initialize the model (assuming it is set) (...) # Get the selected model and connect the signal ().(self.handle_selection) def handle_selection(self, selected: QItemSelection, deselected: QItemSelection): # Handle new selected areas for range in selected: top_left = () bottom_right = () print(f"Select the range: OK {top_left.row()}-{bottom_right.row()}, List {top_left.column()}-{bottom_right.column()}") app = QApplication([]) window = MyTableView() () app.exec_()
Application scenarios
- Batch operation: The user selects multiple lines of data and performs deletion/export.
- Highlight: Dynamically change cell style according to the selected area.
- Data linkage: The data is synchronized between the master and slave views by selecting the status.
Things to note
-
Index validity:
- When ensuring operation
QModelIndex
Valid (if the model is not reset).
- When ensuring operation
-
Performance optimization:
- Avoid frequent operations when processing large data
QItemSelection
, you can merge and update the view.
- Avoid frequent operations when processing large data
-
Signal and slot:
- use
selectionChanged
When signaling, distinguishselected
(Newly selected) anddeselected
(Unselect) parameter.
- use
ByQItemSelection
, can efficiently manage complex user selection behaviors and provide flexible data interaction capabilities for model/view applications.
This is the end of this article about the summary of the use of the PyQt5 QItemSelection class. For more related content of PyQt5 QItemSelection, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!