Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtGui QListWidget adding new item problem
Qt 6.11 is out! See what's new in the release blog

QtGui QListWidget adding new item problem

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 5.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mshemuni
    wrote on last edited by
    #1

    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: s

    class 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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      messi
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        notorious_sos
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved