Adding items to QlistView

后端 未结 1 536
既然无缘
既然无缘 2021-01-12 02:59

I\'m using pyqt4 with python 2.7 and I have a list view widget that I can\'t add items to it

# -*- coding: utf-8 -*-

# Form implementation generated from r         


        
相关标签:
1条回答
  • 2021-01-12 03:29

    QListWidget is a class of higher level that makes the developer can handle it easily, for example QListWidget has a model of type QStantandardItemModel that can not be accessed, in addition to built the QListWidgetItem to handle the data, as you have seen is simple add data through functions like addItem() or addItems().

    On the other hand QListView, from which QListWidget inherits, is of lower level, where you can customize many things, using custom models, etc.

    You can use both:

    QListView

    self.listView = QtGui.QListView(Dialog)
    self.listView.setObjectName(_fromUtf8("listView"))
    
    entries = ['one','two', 'three']
    
    model = QtGui.QStandardItemModel()
    self.listView.setModel(model)
    
    for i in entries:
        item = QtGui.QStandardItem(i)
        model.appendRow(item)
    
    self.gridLayout.addWidget(self.listView, 1, 0, 1, 2)
    

    QListWidget

    self.listwidget = QtGui.QListWidget(Dialog)
    
    entries = ['one','two', 'three']
    
    self.listwidget.addItems(entries)
    
    self.gridLayout.addWidget(self.listwidget, 1, 0, 1, 2)
    
    0 讨论(0)
提交回复
热议问题