向 QlistView 添加项目

Adding items to QlistView

我在 python 2.7 中使用 pyqt4,我有一个列表视图小部件,我无法向其中添加项目

# -*- 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_())

正如你们所见,我使用了

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

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

但是它给了我一个关于参数和数据类型的错误:

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]

还有,这里用ListView好还是我应该用listwidget?

总的来说,两者有什么区别!!

QListWidget是一个更高级别的class,开发者可以轻松处理,例如QListWidget有一个QStantandardItemModel类型的模型,不能访问,除了构建 QListWidgetItem 来处理数据之外,正如您所见,通过 addItem()addItems().

等函数简单地添加数据

另一方面,QListWidget 继承的 QListView 级别较低,您可以在其中自定义很多东西,使用自定义模型等。

您可以同时使用:

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)