Learn Python PyQt

QListWidget

The QListWidget class is an entry-based interface for adding or removing entries from a list, each entry in the list is a QListWidgetItem object, and QListWidget can be set to multi-select.

Book: Create Desktop Apps with Python PyQt5

ListWidget

The program shows the QListWidget example. It adds several items using the method addItem().

qlistwidget

If you click on a widget item, it will show a popup. A clicked event will be sent and it shows a QMessageBox.

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class ListWidget(QListWidget):
    def clicked(self, item):
        QMessageBox.information(self, "ListWidget", "ListWidget: " + item.text())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    listWidget = ListWidget()

    listWidget.resize(300, 120)
    listWidget.addItem("Item 1")
    listWidget.addItem("Item 2")
    listWidget.addItem("Item 3")
    listWidget.addItem("Item 4")
    listWidget.setWindowTitle('QListwidget Example')
    listWidget.itemClicked.connect(listWidget.clicked)

    listWidget.show()
    sys.exit(app.exec_())
listwidget

This example is similar to the previous QListView implementation, except that the controls used are different, one is QListView and one is QListWidget.

This creates a List Widget:

listWidget = ListWidget()

You can resize it:

listWidget.resize(300, 120)

Add several items:

listWidget.addItem("Item 1")
listWidget.addItem("Item 2")
listWidget.addItem("Item 3")
listWidget.addItem("Item 4")

Connect clicked() to the itemClicked event:

listWidget.itemClicked.connect(listWidget.clicked)

QListWidget has these methods:

  • addItem()
  • addItems()
  • insertItem()
  • clear()
  • setCurrentItem()
  • sortItems()

QListWidget has these events:

  • currentItemChanged
  • itemClicked

QDropEvent

You can set QListWidget to accept drag/drop events. The example below lets you drop from the right QListWidget to the left QListWidget.

drag and drop events in pyqt qlistwidget
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class DropInList(QListWidget):
    def __init__(self):
        super(DropInList,self).__init__()
        self.setAcceptDrops(True)

    def dropEvent(self, QDropEvent):
        source_Widget=QDropEvent.source()
        items=source_Widget.selectedItems()
        print(items)
        for i in items:
            source_Widget.takeItem(source_Widget.indexFromItem(i).row())
            self.addItem(i)
        print('drop event')

class MainWidget(QWidget):
    def __init__(self):
        super(MainWidget,self).__init__()
        self.setWindowTitle('Drag and Drop with QListView')
        self.main_layout=QHBoxLayout()
        self.left_widget=DropInList()
        self.right_widget=QListWidget()
        pre_list=['A','B','C']
        self.right_widget.addItems(pre_list)
        self.right_widget.setDragEnabled(True)
        self.right_widget.setDragDropOverwriteMode(False)
        self.right_widget.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.right_widget.setDefaultDropAction(Qt.MoveAction)
        self.main_layout.addWidget(self.left_widget)
        self.main_layout.addWidget(self.right_widget)
        self.setLayout(self.main_layout)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    m=MainWidget()
    m.show()
    sys.exit(app.exec_())

Book: Create Desktop Apps with Python PyQt5