将一批数据list加入到QListView
slm = QStringListModel() # 创建modelslm.setStringList(['111', '222', '333']) # 将数据设置到modelself.listView.setModel(slm)
设置某行被选中,这里以第一行选中为例子
model_index = slm.index(0, 0)self.listView.setCurrentIndex(model_index)slm.setData(model_index, (255, 0, 0), Qt.BackgroundColorRole)
修改某一行(比如第2行改成aaaaa)
slm = QStringListModel() # 创建model
slm.setStringList(['111', '222', '333']) # 将数据设置到model
self.listView.setModel(slm)
slm.setData(slm.index(1), "aaaaa")
删除某行
indexes = self.listView.selectedIndexes()if indexes:index = indexes[0]slm.removeRow(index.row())
给QListView添加一行数据
row = slm.rowCount()value = '444'slm.insertRow(row)slm.setData(slm.index(row), value)
给ListView添加右键菜单
self.listView.setContextMenuPolicy(3)self.listView.customContextMenuRequested[QPoint].connect(self.listWidgetContext)def listWidgetContext(self, point):popMenu = QMenu()popMenu.addAction("添加")popMenu.addAction("修改")popMenu.addAction("删除")popMenu.exec_(QCursor.pos())
清空QListView
slm = QStringListModel() # 创建modelslm.setStringList([]) # 将数据设置到modelself.listView.setModel(slm)
给QListView每一行前面加个小图标
data_list = [{'img': "football.png", "title": "足球"},{"img": "basketball.png", "title": "篮球"},{"img": "valleyball.png", "title": "排球"}]model = QStandardItemModel()for data in self.data_list:item = QStandardItem(QIcon(data["img"]), data["title"])model.appendRow(item)self.listView.setModel(model)
下面给个完整代码大家可以直接跑起来
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'listviewtest.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QStringListModel, Qt, QPoint
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import QMenuclass Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(800, 600)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.listView = QtWidgets.QListView(self.centralwidget)self.listView.setGeometry(QtCore.QRect(100, 30, 551, 441))self.listView.setObjectName("listView")slm = QStringListModel() # 创建modelslm.setStringList(['111', '222', '333']) # 将数据设置到modelself.listView.setModel(slm)# 设置选择某行model_index = slm.index(0, 0)self.listView.setCurrentIndex(model_index)slm.setData(model_index, (255, 0, 0), Qt.BackgroundColorRole)# 删除某行indexes = self.listView.selectedIndexes()if indexes:index = indexes[0]slm.removeRow(index.row())# 添加某行row = slm.rowCount()value = '444'slm.insertRow(row)slm.setData(slm.index(row), value)# 添加右键菜单self.listView.setContextMenuPolicy(3)self.listView.customContextMenuRequested[QPoint].connect(self.listWidgetContext)# 清空列表# slm = QStringListModel() # 创建model# slm.setStringList([]) # 将数据设置到model# self.listView.setModel(slm)MainWindow.setCentralWidget(self.centralwidget)self.menubar = QtWidgets.QMenuBar(MainWindow)self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 30))self.menubar.setObjectName("menubar")MainWindow.setMenuBar(self.menubar)self.statusbar = QtWidgets.QStatusBar(MainWindow)self.statusbar.setObjectName("statusbar")MainWindow.setStatusBar(self.statusbar)self.retranslateUi(MainWindow)QtCore.QMetaObject.connectSlotsByName(MainWindow)def listWidgetContext(self, point):popMenu = QMenu()popMenu.addAction("添加")popMenu.addAction("修改")popMenu.addAction("删除")popMenu.exec_(QCursor.pos())def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))if __name__ == "__main__":import sysapp = QtWidgets.QApplication(sys.argv)MainWindow = QtWidgets.QMainWindow()ui = Ui_MainWindow()ui.setupUi(MainWindow)MainWindow.show()sys.exit(app.exec_())