我正在使用pyqt4与python 2.7,我有一个列表视图小部件,我不能添加项目
# -*- 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?
一般来说,两者之间有什么区别!!
答案 0 :(得分:2)
QListWidget
是一个更高级别的类,使开发人员可以轻松处理它,例如QListWidget
除了构建之外,还有一个无法访问的类型QStantandardItemModel
的模型处理数据的QListWidgetItem
正如您所见,只需通过addItem()
或addItems()
等功能添加数据。
另一方面,QListView
继承的QListWidget
属于较低级别,您可以使用自定义模型等自定义许多内容。
您可以同时使用:
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)
self.listwidget = QtGui.QListWidget(Dialog)
entries = ['one','two', 'three']
self.listwidget.addItems(entries)
self.gridLayout.addWidget(self.listwidget, 1, 0, 1, 2)