In this PyQt5 article we are going to learn about PyQt5 Simple Project With QListWidget, so first of all you should have a basics understanding of QListWidget. OK the project that we are going make is Simple QListWidget Project by that we can add items to QListWidget, we can update the items, delete the items and sort the items, so lets get started. PyQt5 QListWidget is user interface (UI) widget in PyQt5 library, which is used to display list of items to the user. it is subclass of the QListView class and provides a 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 the 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.
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 we need to import some required classes.
1 2 3 |
from PyQt5.QtWidgets import QDialog, QListWidget,QLineEdit , QMessageBox , QVBoxLayout, QInputDialog,QPushButton, QHBoxLayout, QApplication from PyQt5 import QtGui import sys |
After that we create our Programming Dialog class that extends from QDialog , and in the constructor we add the requirements for the project so this is the complete code you can check the code and also for more information you can watch the complete video for this
This is the complete code for this article
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
from PyQt5.QtWidgets import QDialog, QListWidget,QLineEdit , QMessageBox , QVBoxLayout, QInputDialog,QPushButton, QHBoxLayout, QApplication from PyQt5 import QtGui import sys class ProgrammingDialog(QDialog): def __init__(self,name, proList = None): super(ProgrammingDialog, self).__init__() self.name = name self.list = QListWidget() if proList is not None: self.list.addItems(proList) self.list.setCurrentRow(0) vbox = QVBoxLayout() for text, slot in (("Add", self.add), ("Edit", self.edit), ("Remove", self.remove), ("Sort", self.sort), ("Close", self.close)): button= QPushButton(text) vbox.addWidget(button) button.clicked.connect(slot) hbox = QHBoxLayout() hbox.addWidget(self.list) hbox.addLayout(vbox) self.setLayout(hbox) self.setWindowTitle("Edit {0} Programming List".format(self.name)) self.setWindowIcon(QtGui.QIcon("icon.png")) def add(self): row = self.list.currentRow() title = "Add {0}".format(self.name) string, ok = QInputDialog.getText(self, title, title) if ok and string is not None: self.list.insertItem(row, string) def edit(self): row = self.list.currentRow() item = self.list.item(row) if item is not None: title = "Edit {0}".format(self.name) string, ok = QInputDialog.getText(self, title, title, QLineEdit.Normal, item.text()) if ok and string is not None: item.setText(string) def remove(self): row = self.list.currentRow() item = self.list.item(row) if item is None: return reply = QMessageBox.question(self, "Remove {0}".format( self.name), "Remove {0} `{1}'?".format( self.name, str(item.text())), QMessageBox.Yes|QMessageBox.No) if reply == QMessageBox.Yes: item = self.list.takeItem(row) del item def sort(self): self.list.sortItems() def close(self): self.accept() if __name__ == "__main__": programming = ["Python", "Java", "C#", "C++", "Ruby", "Kotlin"] app= QApplication(sys.argv) dialog = ProgrammingDialog("Languages", programming) dialog.exec_() |
So in the above code this is our main class that extends from QDialog. in the class we have created the object of our QListWidget. also we have created QVBoxLayout with some methods for our 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 |
class ProgrammingDialog(QDialog): def __init__(self,name, proList = None): super(ProgrammingDialog, self).__init__() self.name = name self.list = QListWidget() if proList is not None: self.list.addItems(proList) self.list.setCurrentRow(0) vbox = QVBoxLayout() for text, slot in (("Add", self.add), ("Edit", self.edit), ("Remove", self.remove), ("Sort", self.sort), ("Close", self.close)): button= QPushButton(text) vbox.addWidget(button) button.clicked.connect(slot) |
OK, we have created the VBoxLayout but now we need an HBoxLayout, this layout will be our main layout because we are going to add our VBoxLayout to this HBoxLayout. also we add our QListWidget in this HBoxLayout.
1 2 3 4 |
hbox = QHBoxLayout() hbox.addWidget(self.list) hbox.addLayout(vbox) self.setLayout(hbox) |
In here we set our window title with icon.
1 2 |
self.setWindowTitle("Edit {0} Programming List".format(self.name)) self.setWindowIcon(QtGui.QIcon("icon.png")) |
Run the complete code and this is the result
Also you can watch the complete video for this article.
Subscribe and Get Free Video Courses & Articles in your Email
Hey great work over there, am still new in PyQt and i was wondering if i want to display the length of the list in realtime (updates whenever a language is added or deleted.)
How do i achieve that