pyqt QListView详细用法
作者:互联网
像歌曲列表,文件列表,这种可以用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.onClickedListView)
layout.addWidget(listview)
self.setLayout(layout)
def onClickedListView(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_())
单击某一项时会触发信号clicked, 该信号如下:
def clicked(self, QModelIndex): # real signature unknown; restored from __doc__
""" clicked(self, QModelIndex) [signal] """
pass
QModelIndex保存了当前点击的行信息,可以通过row()获取当前的项索引。
上面代码链接了该信号
listview.clicked.connect(self.onClickedListView)
点击后,就会触发槽函数,获取当前行信息。
标签:__,QListView,clicked,self,pyqt,用法,列表,listview 来源: https://blog.51cto.com/u_12570763/2855315