The list component QListView is a class derived from QAbstractItemView and implements the interface defined by the QAbstractItemView class. It is one of the view classes in the Model/View architecture and a part of the Model/View framework. It provides model-based list mode or icon mode views. It displays the items stored in the model as a simple list or icon collection without parent-child hierarchy, and the view does not display horizontal or vertical titles. The common methods are as follows:
method | describe |
setModel() | Used to set the Model associated with the View. You can use Python's native list as the data source Model; |
selectedItem() | Select the entry of Model; |
isSelected() | Judge whether an entry in the Model is selected; |
setViewMode() | Set the view mode. ListMode: items are arranged from top to bottom, and displayed in small size; IconMode: items are arranged from left to right, and displayed in large size; |
Common event types are shown in the following table:
Event type | describe |
clicked | This event is triggered when an item is clicked; |
doubleClicked | This event is triggered when you double-click an item; |
activated | This event is triggered when the item specified by index is activated; |
entered | This event is triggered when the mouse cursor enters the item specified by index; |
iconSizeChanged | This event is triggered when the icon size is set when the view is visible; |
indexesMoved | This event is triggered when the index is moved in the view; |
pressed | This event is triggered when the mouse button is pressed; |
viewportEntered | This event is triggered when the mouse cursor enters the view. |
Program list: listview.py
import sys from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget, QHBoxLayout, QListView from PyQt5.QtGui import QIcon, QStandardItem, QStandardItemModel # Inherit QWidget class ListView(QWidget): sports = [{'img': "football.png", "title": "Football"}, {"img": "basketball.png", "title": "Basketball"}, {"img": "valleyball.png", "title": "Volleyball"}] list_view = 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.list_view = QListView() model = QStandardItemModel() # Input content for sport in self.sports: item = QStandardItem(QIcon(sport["img"]), sport["title"]) model.appendRow(item) self.list_view.setModel(model) layout.addWidget(self.list_view) # Click event self.list_view.clicked.connect(self.list_click) self.setLayout(layout) # resize window self.resize(900, 500) # Center window self.center() # Window title self.setWindowTitle("QListView application") # Display window self.show() # Get folder path def list_click(self, index): # Gets the column of the selected text item = self.sports[index.row()]["title"] print(item) # 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 = ListView() sys.exit(app.exec_())
After running the program, the pop-up window is as follows:
Well, that's all for QListView. 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