>>> 点击进入:pyqt5专栏<<<
老师原课件下载地址:
有积分的朋友,支持下我,打赏也OK。
不下载也可以,我每节课会吧代码贴出来
P64课时65.显示列数据(QListView控件)
参考博客:
PyQt学习随笔:Qt中tem Views(Model-Based)和Item Widgets(Item-Based)控件的用途和关系
PyQt5高级界面控件之QListWidget(三)
'''
显示列表数据(QListView控件)
'''
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QListView, QMessageBox
from PyQt5.QtCore import QStringListModel
import sys
class ListViewDemo(QWidget):
def __init__(self, parent=None):
super(ListViewDemo, self).__init__(parent)
self.setWindowTitle("QListView 例子")
self.resize(300, 270)
layout = QVBoxLayout()
listview = QListView()
listModel = QStringListModel()
self.list = ["列表项1","列表项2", "列表项3"]
listModel.setStringList(self.list)
listview.setModel(listModel)
listview.clicked.connect(self.clicked)
layout.addWidget(listview)
self.setLayout(layout)
def clicked(self,item):
QMessageBox.information(self,"QListView","您选择了:" + self.list[item.row()])
if __name__ == "__main__":
app = QApplication(sys.argv)
win = ListViewDemo()
win.show()
sys.exit(app.exec_())
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'child', 'column', 'data', 'flags', 'internalId', 'internalPointer', 'isValid', 'model', 'parent', 'row', 'sibling', 'siblingAtColumn', 'siblingAtRow']
只有widget才能手动添加item
# -*- coding:utf-8 -*-
'''
@Author: knocky
@Blog: https://blog.csdn.net/zzx188891020
@E-mail: [email protected]
@File: class65.py
@CreateTime: 2020/5/29 23:32
'''
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox
from PyQt5 import uic
class my_form(QWidget):
def __init__(self):
super().__init__()
uic.loadUi('../ui_package/class65.ui', self)
self.listWidget.clicked.connect(self.clicked)
def clicked(self, item):
# 这里就是想做个测试
s = "data:{},column:{},row:{}\ninternalId:{},model:{},\nflags:{}"
QMessageBox.information(self, "弹框标题", s.format(item.data(), item.column(), item.row(),
item.internalId(), item.model(), item.flags()))
if __name__ == '__main__':
app = QApplication(sys.argv)
main = my_form()
main.show()
sys.exit(app.exec_())