Python Qt学习(七)Listview

源代码:

# -*- 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 sys

class 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.translate
        MainWindow.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=0
        msg=""
        while self.model.item(i):
            if self.model.item(i).checkState():
                print("hell0")
                msg=msg+self.model.item(i).text()+"\r\n"
            i+=1

        self.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())        

截图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值