源代码:
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'qt_listview.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# 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.QtWidgets import QApplication
import sysclass Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(800, 513)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.listView = QtWidgets.QListView(self.centralwidget)self.listView.setGeometry(QtCore.QRect(10, 10, 311, 431))self.listView.setObjectName("listView")self.pushButton = QtWidgets.QPushButton(self.centralwidget)self.pushButton.setGeometry(QtCore.QRect(370, 30, 131, 28))self.pushButton.setObjectName("pushButton")self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)self.pushButton_2.setGeometry(QtCore.QRect(370, 80, 131, 28))self.pushButton_2.setObjectName("pushButton_2")self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)self.pushButton_3.setGeometry(QtCore.QRect(370, 130, 131, 28))self.pushButton_3.setObjectName("pushButton_3")MainWindow.setCentralWidget(self.centralwidget)self.menubar = QtWidgets.QMenuBar(MainWindow)self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))self.menubar.setObjectName("menubar")MainWindow.setMenuBar(self.menubar)self.statusbar = QtWidgets.QStatusBar(MainWindow)self.statusbar.setObjectName("statusbar")MainWindow.setStatusBar(self.statusbar)self.pushButton.clicked.connect(self.initListView)self.pushButton_2.clicked.connect(self.getCurrentItem)self.pushButton_3.clicked.connect(self.getAllSelectedItem)self.model= QtGui.QStandardItemModel()self.listView.setModel(self.model)self.retranslateUi(MainWindow)QtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))self.pushButton.setText(_translate("MainWindow", "Init List View"))self.pushButton_2.setText(_translate("MainWindow", "Get Current Item"))self.pushButton_3.setText(_translate("MainWindow", "Get All Selected Item"))def show(self, Form):Form.show()def initListView(self):item_list=['line 1', 'line 2', 'line 3']for i in item_list:item=QtGui.QStandardItem(i)item.setCheckable(True)self.model.appendRow(item)def getCurrentItem(self):data=self.listView.currentIndex().data()index=self.listView.currentIndex().row()if index>=0:msg= "Current Selected Item Data = " + data + "\r\n" + "Current Selected Item Index="+str(index)self.messageBox(msg)def messageBox(self,msg):msgBox = QtWidgets.QMessageBox()msgBox.setIcon(QtWidgets.QMessageBox.Icon.Information)msgBox.setWindowTitle("Qt Message Box")msgBox.setText(msg)msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel)returnValue = msgBox.exec() def getAllSelectedItem(self):i=0msg=""while self.model.item(i):if self.model.item(i).checkState():print("hell0")msg=msg+self.model.item(i).text()+"\r\n"i+=1self.messageBox(msg)if __name__ == "__main__":app = QApplication(sys.argv)Form=QtWidgets.QMainWindow() main_win = Ui_MainWindow() main_win.setupUi(Form)main_win.show(Form)sys.exit(app.exec())
截图: