This is a static archive of the Python wiki, which was retired in February 2026 due to lack of usage and the resources necessary to serve it — predominately to bots, crawlers, and LLM companies.
Pages are preserved as they were at the time of archival. For current information, please visit python.org.
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list.

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 *
   3 
   4 app = QApplication(sys.argv)
   5 
   6 listWidget = QListWidget()
   7 
   8 for i in range(10):
   9     item = QListWidgetItem("Item %i" % i)
  10     listWidget.addItem(item)
  11 
  12 listWidget.show()
  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.


2026-02-14 16:12