In this PyQt5 article i want to show you creating of QListWidget Practical Example in PyQt5. PyQt5 QListWidget is user interface (UI) widget in the PyQt5 library, which is used to display list of items to the user. It is subclass of the QListView class and provides convenient way to display a list of items that the user can interact with.
QListWidget can display items in vertical or horizontal list format, and items can be customized to include icons, text and additional widgets. QListWidget also provides builtin support for drag and drop operations, which makes it easy for users to reorder or move items within the list.
some common use cases for QListWidget include displaying list of files or images for the user to select from, displaying list of items in shopping cart or order summary, or displaying list of options for the user to choose from in a settings menu.
to use QListWidget in PyQt5 application, you would first create an instance of the QListWidget class and add items to it using the addItem() method. you can also customize appearance of the items using the setItemWidget() method, which allows you to add additional widgets to each item in the list.
In result we can say that PyQt5 QListWidget is a useful UI widget that can help you create intuitive and interactive user interfaces in your PyQt5 applications.
Also you can check more Python GUI articles in the below links
- Kivy GUI Development Tutorials
- TKinter GUI Development Tutorials
- Psyide2 GUI Development
- wxPython GUI Development Tutorials
- PyQt5 GUI Development Tutorials
First of all we need some imports
1 2 3 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QListWidget, QLabel, QVBoxLayout import sys |
After that we are going to create our Main window that extends from QWidget and we add the required items for our window like title, icon geometry and etc. also we call our initWindow() method in this class.
1 2 3 4 5 6 7 8 9 |
class Window(QWidget): def __init__(self): super().__init__() self.title = "PyQt5 QListWidget" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.InitWindow() |
These line of codes are for setting our window title, icon and geometry.
1 2 3 |
self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) |
So in here we have created QVBoxLayout with QListWidget and we added some items to our QListWidget.
1 2 3 4 5 6 7 8 |
vbox= QVBoxLayout() self.list = QListWidget() self.list.insertItem(0, "Python") self.list.insertItem(1, "Java") self.list.insertItem(2, "C++") self.list.insertItem(3, "C#") self.list.insertItem(4, "Ruby") self.list.insertItem(5, "Kotlin") |
In here we have connected the clicked signal of listwidget with the slot or method that we will create in the later.
1 |
self.list.clicked.connect(self.listview_clicked) |
So now this is the method or slot that we have already connected to the clicked signal.
1 2 3 |
def listview_clicked(self): item = self.list.currentItem() self.label.setText(str(item.text())) |
Also every PyQt5 application must create an application object.
1 |
App = QApplication(sys.argv) |
Finally, we enter the mainloop of the application. The event handling starts from this point.
1 2 |
window = Window() sys.exit(App.exec()) |
Complete source code for QListWidget
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QListWidget, QLabel, QVBoxLayout import sys class Window(QWidget): def __init__(self): super().__init__() self.title = "PyQt5 QListWidget" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.InitWindow() def InitWindow(self): self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) vbox= QVBoxLayout() self.list = QListWidget() self.list.insertItem(0, "Python") self.list.insertItem(1, "Java") self.list.insertItem(2, "C++") self.list.insertItem(3, "C#") self.list.insertItem(4, "Ruby") self.list.insertItem(5, "Kotlin") self.list.clicked.connect(self.listview_clicked) vbox.addWidget(self.list) self.label = QLabel() self.label.setFont(QtGui.QFont("Sanserif", 15)) vbox.addWidget(self.label) self.setLayout(vbox) self.show() def listview_clicked(self): item = self.list.currentItem() self.label.setText(str(item.text())) App = QApplication(sys.argv) window = Window() sys.exit(App.exec()) |
Run code and this is the result
Subscribe and Get Free Video Courses & Articles in your Email