Adding items to a list widget

On the #pyqt channel at Freenode, afief asked for an example that showed how to add items to a list widget using QListWidgetItem rather than just plain strings.

   1 import sys
   2 from PyQt4.QtGui import *
   4 app = QApplication(sys.argv)
   6 listWidget = QListWidget()
   8 for i in range(10):
   9     item = QListWidgetItem("Item %i" % i)
  10     listWidget.addItem(item)
  13 sys.exit(app.exec_())

Although this example seems easy, there is one subtle point worth noting: the list widget takes ownership of each of the items added to it. This means that you cannot add an item to more than one list widget.

PyQt/Adding items to a list widget (last edited 2014-06-07 16:43:55 by DavidBoddie)

Unable to edit the page? See the FrontPage for instructions.