Adding items to QlistView

Ahmed Wagdi picture Ahmed Wagdi · Mar 20, 2018 · Viewed 34.7k times · Source

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 reading ui file 'add_category.ui'
#
# Created: Mon Mar 19 23:22:30 2018
#      by: PyQt4 UI code generator 4.10
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog1(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(608, 460)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/media/media/in.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        Dialog.setWindowIcon(icon)
        self.gridLayout = QtGui.QGridLayout(Dialog)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.lineEdit = QtGui.QLineEdit(Dialog)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setIcon(icon)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.gridLayout.addWidget(self.pushButton, 0, 6, 1, 1)
        self.label = QtGui.QLabel(Dialog)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Adobe Arabic"))
        font.setPointSize(24)
        self.label.setFont(font)
        self.label.setObjectName(_fromUtf8("label"))
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
        self.listView = QtGui.QListView(Dialog)
        self.listView.setObjectName(_fromUtf8("listView"))
        entries = ['one','two', 'three']

        for i in entries:
            item = QtGui.QListView(i)
            self.listView.addItem(item)
        self.gridLayout.addWidget(self.listView, 1, 0, 1, 2)
        self.pushButton_2 = QtGui.QPushButton(Dialog)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8(":/media/media/ok.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_2.setIcon(icon1)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        def go_back(self):
            Dialog.hide()
        self.pushButton_2.clicked.connect(go_back)
        self.gridLayout.addWidget(self.pushButton_2, 2, 1, 1, 1)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "إضافة فئة", None))
        self.lineEdit.setPlaceholderText(_translate("Dialog", "هنا يكتب الاسم الفئة الجديدة", None))
        self.pushButton.setText(_translate("Dialog", "إضافة", None))
        self.label.setText(_translate("Dialog", "إسـم الفئة الجديدة", None))
        self.pushButton_2.setText(_translate("Dialog", "موافق", None))

import resrcs

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog1()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

As you guys can see i used

entries = ['one','two', 'three']

for i in entries:
    item = QtGui.QListView(i)
    self.listView.addItem(item)

But it gave me an error that is talking about arguments and data types:

Traceback (most recent call last):
  File "C:\python\townoftechwarehouse\add_category.py", line 84, in <module>
    ui.setupUi(Dialog)
  File "C:\python\townoftechwarehouse\add_category.py", line 54, in setupUi
    item = QtGui.QListView(i)
TypeError: QListView(QWidget parent=None): argument 1 has unexpected type 'str'
[Finished in 1.7s]

Also, is it right to use ListView here or I should use listwidget?

In general, what is the difference between both !!

Answer

eyllanesc picture eyllanesc · Mar 20, 2018

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)