python pyqt5 QListView

2022-01-10 08:40:22 浏览数 (1)

方法

image.png

image.png

-- coding: utf-8 --

''' 【简介】 PyQt5中 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()

代码语言:javascript复制
    listView = QListView()
    slm = QStringListModel();
    self.qList = ['Item 1', 'Item 2', 'Item 3', 'Item 4']
    slm.setStringList(self.qList)
    listView.setModel(slm)
    listView.clicked.connect(self.clicked)
    layout.addWidget(listView)
    self.setLayout(layout)

def clicked(self, qModelIndex):
    QMessageBox.information(self, "QListView", "你选择了: "   self.qList[qModelIndex.row()])

if name == "main": app = QApplication(sys.argv) win = ListViewDemo() win.show() sys.exit(app.exec_())

0 人点赞