QtGui QListWidget adding new item problem
-
Hi everyone.
At first:
-
My sys.version output:
bq. 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3]bq. -
My OS:
bq. Ubuntu
Release 12.04 (precise) 32-bit
I used Qt 4 Designer for my GUI. And made a form contains a QListWidget and a QPushButton.
I want to add numbers to list widget when button clicked. I wrote the code below. It can add a blank row and fill it with firs number. In second loop it add a blank row and print this error:@Traceback (most recent call last:(
File "b.py", line 48, in selectFile
self.listWidget.item = self.listWidget.item(x)
TypeError: 'QListWidgetItem' object is not callable@Here is my code:
@# -- coding: utf-8 --Form implementation generated from reading ui file 'untitled2.ui'
Created: Fri Aug 31 04:05:57 2012
by: PyQt4 UI code generator 4.9.1
WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: sclass Ui_Form(object:(
def setupUi(self, Form:(
Form.setObjectName(_fromUtf8("Form"))
Form.resize(658, 527)
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(170, 260, 89, 27))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.listWidget = QtGui.QListWidget(Form)
self.listWidget.setGeometry(QtCore.QRect(150, 20, 256, 192))
self.listWidget.setObjectName(_fromUtf8("listWidget"))self.retranslateUi(Form) QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.selectFile) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form:( Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) self.pushButton.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8)) __sortingEnabled = self.listWidget.isSortingEnabled() self.listWidget.setSortingEnabled(False) self.listWidget.setSortingEnabled(__sortingEnabled) def selectFile(self:( for x in range(0,5:( item=QtGui.QListWidgetItem() self.listWidget.addItem(item) self.listWidget.item = self.listWidget.item(x) self.listWidget.item.setText(QtGui.QApplication.translate("Form", str(x), None, QtGui.QApplication.UnicodeUTF8)) print("Worked for x="+str(x))
if name == "main":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
@What's the problem?
Please help me.
Sorry for bad English. -
-
Hi
I think this is a PyQt problem. In native C++ I have never encounter such a problem.
I would suggest that you have to ask the question here again:http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Good luck.
-
Hi,
what you do by:
@self.listWidget.item = self.listWidget.item(x)@
is replace the method .item() of self.listWidget by the one Item you select in the first loop.
if you use:
@
itemA = self.listWidget.item(x)
itemA.setText(QtGui.QApplication.translate("Form", str(x), None, QtGui.QApplication.UnicodeUTF8))
@It should work
Bon chance