1

Studying online examples I managed to get a custom widget added to a listbox in PyQT4, this is the code for the custom widget progress.py:

from PyQt4 import QtCore, QtGui, QtWebKit

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_(QtGui.QWidget):
    def __init__(self, name, parent=None):
        super(QtGui.QWidget,self).__init__(parent)
        self.layoutWidget = QtGui.QWidget()
        self.layoutWidget.setGeometry(QtCore.QRect(0, 0, 611, 31))
        self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
        self.gridLayout = QtGui.QGridLayout(self.layoutWidget)
        self.gridLayout.setMargin(0)
        self.gridLayout.setSpacing(10)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))

        self.progressBar = QtGui.QProgressBar(self.layoutWidget)
        self.progressBar.setProperty("value", 0)
        self.progressBar.setObjectName(_fromUtf8("progressBar"))
        self.gridLayout.addWidget(self.progressBar, 0, 1, 1, 1)
        self.gridLayout.addWidget(self.graphicsView, 1, 1, 1, 1)

        self.webView = QtWebKit.QWebView(self.layoutWidget)
        self.webView.setGeometry(QtCore.QRect(0, 0, 500, 200))
        self.webView.setUrl(QtCore.QUrl(_fromUtf8("about:blank")))
        self.webView.setObjectName(_fromUtf8("webView"))
        self.gridLayout.addWidget(self.webView, 1, 1, 1, 1)


        QtCore.QMetaObject.connectSlotsByName(self)
        #self.label.setText(_translate("", name, None))
        self.webView.setHtml(name)
        self.setLayout(self.gridLayout)

I am using the below code to add and populate this custom widget from main.py as:

def on_info_ready(self, data):
      info = data.split("\n")
      item = QtGui.QListWidgetItem(self.ui.listWidget)
      makeHtml = "<h1>" + info[1] + "</h1>"
      item_widget = progress.Ui_(makeHtml)
      item.setSizeHint(item_widget.sizeHint())
      self.ui.listWidget.addItem(item)
      self.ui.listWidget.setItemWidget(item,item_widget)

The above works perfectly, but I am unable to figure out how to access the progressbar control in above added custom widget appended as listitems. I tried multiple variations of the below but it fails :

  def on_progress(self, data):
      print (data)
      self.ui.listWidget.item(0).progressBar.setValue(data)

The above code errors out as :

Traceback (most recent call last):
  File "C:\Users\kk\Desktop\py4 gui\main.py", line 105, in on_progress
    self.ui.listWidget.item(0).progressBar.setValue(data)
AttributeError: 'QListWidgetItem' object has no attribute 'progressBar'
0.0
Traceback (most recent call last):
  File "C:\Users\kk\Desktop\py4 gui\main.py", line 105, in on_progress
    self.ui.listWidget.item(0).progressBar.setValue(data)
AttributeError: 'QListWidgetItem' object has no attribute 'progressBar'
0.0

1 Answer 1

1

Use QListWidget.itemWidget to get the widget you set using QListWidget.setItemWidget.

Replace following line:

self.ui.listWidget.item(0).progressBar.setValue(data)

with:

widget = self.ui.listWidget.itemWidget(self.ui.listWidget.item(0))
widget.progressBar.setValue(data)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.