In this Python PyQt5 lesson we want to learn about Python PyQt5 QListWidget, so QListWidget is similar to ListView that is supplied by QListView. but with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list.
Now open your Qt Designer, you can just write pyqt5designer in your terminal, after opening the Qt Designer you need to create Widget window. now we add widgets in Qt Designer.
- Add a VBoxLayout in the Designer
- Add QListWidget at the top and a QLabel at the bottom of the VBoxLayout
You can right click on the listwidget and choose Change Stylesheet and add this css style.
1 2 3 4 5 6 7 8 |
QListWidget { font: 75 14pt "MS Shell Dlg 2"; background-color:rgb(255, 0, 127) } |
Also you can do the same for the QLabel.
1 2 3 4 5 6 |
QLabel { color:red; } |
This is the design.
After completing the design you need to save the .ui file, iam going to name it listwidget.ui, now copy the file and paste it in the Scripts folder of your Python installation, because we want to convert our ui file in to python file and for converting you need to use pyuic5 module. pyuic5 module is located in the Scripts folder of your Python installation, run this command for converting in your terminal.
1 |
pyuic5 listwidget.ui -o listwidget.py -x |
And this is the converted code also we have added our clicked() signal with the method that we want to connect.
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 100 101 102 103 104 105 |
# -*- coding: utf-8 -*- # Form implementation generated from reading # ui file 'listwidget.ui' # # Created by: PyQt5 UI code generator 5.15.1 # # WARNING: Any manual changes made to this file # will be lost when pyuic5 is # run again. Do not edit this file unless you know # what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(426, 329) self.verticalLayout_2 = QtWidgets.QVBoxLayout(Form) self.verticalLayout_2.setObjectName("verticalLayout_2") self.verticalLayout = QtWidgets.QVBoxLayout() self.verticalLayout.setObjectName("verticalLayout") self.listWidget = QtWidgets.QListWidget(Form) self.listWidget.setStyleSheet("QListWidget {\n" "\n" " font: 75 14pt \"MS Shell Dlg 2\";\n" "background-color:rgb(255, 0, 127)\n" "\n" "\n" "\n" "}") self.listWidget.setObjectName("listWidget") item = QtWidgets.QListWidgetItem() self.listWidget.addItem(item) item = QtWidgets.QListWidgetItem() self.listWidget.addItem(item) item = QtWidgets.QListWidgetItem() self.listWidget.addItem(item) item = QtWidgets.QListWidgetItem() self.listWidget.addItem(item) item = QtWidgets.QListWidgetItem() self.listWidget.addItem(item) item = QtWidgets.QListWidgetItem() self.listWidget.addItem(item) #clicked signal of listwidget self.listWidget.clicked.connect(self.item_clicked) self.verticalLayout.addWidget(self.listWidget) self.label = QtWidgets.QLabel(Form) font = QtGui.QFont() font.setPointSize(14) font.setBold(True) font.setWeight(75) self.label.setFont(font) self.label.setStyleSheet("QLabel {\n" "\n" "color:red;\n" "\n" "\n" "}") self.label.setText("") self.label.setObjectName("label") self.verticalLayout.addWidget(self.label) self.verticalLayout_2.addLayout(self.verticalLayout) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) #method or slot that is connected to signal def item_clicked(self): item = self.listWidget.currentItem() self.label.setText("You have selected : " + str(item.text())) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) __sortingEnabled = self.listWidget.isSortingEnabled() self.listWidget.setSortingEnabled(False) item = self.listWidget.item(0) item.setText(_translate("Form", "Python")) item = self.listWidget.item(1) item.setText(_translate("Form", "Java")) item = self.listWidget.item(2) item.setText(_translate("Form", "C++")) item = self.listWidget.item(3) item.setText(_translate("Form", "C#")) item = self.listWidget.item(4) item.setText(_translate("Form", "JavaScript")) item = self.listWidget.item(5) item.setText(_translate("Form", "Kotlin")) self.listWidget.setSortingEnabled(__sortingEnabled) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_()) |
This is the method that we have already connected with the clicked signal of listwidget, so in this method first we need to get the item value from the pyqt listwidget and after that we set the value in Python PyQt5 label.
1 2 3 |
def item_clicked(self): item = self.listWidget.currentItem() self.label.setText("You have selected : " + str(item.text())) |
Run the complete code and this is the result.
Creating PyQt5 QListWidget using Coding
OK now we want to create our listwidget using coding, in the previous part we have used Qt Designer, in here we are not going to use Qt Designer. this is the complete source code.
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 |
from PyQt5.QtWidgets import QApplication, QWidget,\ QLabel,QVBoxLayout, QListWidget import sys from PyQt5.QtGui import QIcon, QFont class Window(QWidget): def __init__(self): super().__init__() #window requirements like title,icon self.setGeometry(200,200,400,300) self.setWindowTitle("PyQt5 QListWidget") self.setWindowIcon(QIcon("python.png")) self.create_list() def create_list(self): #create vbox layout object vbox = QVBoxLayout() #create object of list_widget self.list_widget = QListWidget() #add items to the listwidget self.list_widget.insertItem(0, "Python") self.list_widget.insertItem(1, "Java") self.list_widget.insertItem(2, "C++") self.list_widget.insertItem(3, "C#") self.list_widget.insertItem(4, "Kotlin") self.list_widget.setStyleSheet('background-color:red') self.list_widget.setFont(QFont("Sanserif", 15)) self.list_widget.clicked.connect(self.item_clicked) #create label self.label = QLabel("") self.setFont(QFont("Sanserif", 13)) self.setStyleSheet('color:green') #add widgets to the vboxlyaout vbox.addWidget(self.list_widget) vbox.addWidget(self.label) #set the layout for the main window self.setLayout(vbox) def item_clicked(self): item = self.list_widget.currentItem() self.label.setText("You have selected: " + str(item.text())) App = QApplication(sys.argv) window = Window() window.show() sys.exit(App.exec()) |
These are our window requirements like window title, window icon, width and height of the window, x and y position of the window.
1 2 3 |
self.setGeometry(200,200,400,300) self.setWindowTitle("PyQt5 QListWidget") self.setWindowIcon(QIcon("python.png")) |
In here we have created the object of QListWidget.
1 |
self.list_widget = QListWidget() |
Also you need to add some items in the QListWidget using insertItem() method.
1 2 3 4 5 6 7 |
self.list_widget.insertItem(0, "Python") self.list_widget.insertItem(1, "Java") self.list_widget.insertItem(2, "C++") self.list_widget.insertItem(3, "C#") self.list_widget.insertItem(4, "Kotlin") self.list_widget.setStyleSheet('background-color:red') self.list_widget.setFont(QFont("Sanserif", 15)) |
This is the clicked() signal of the listwidget that we have already connected with item_clicked() method.
1 |
self.list_widget.clicked.connect(self.item_clicked) |
This is the method that we have already connected with the clicked signal of listwidget, so in this method first we need to get the item value from the pyqt listwidget and after that we set the value in PyQt5 label.
1 2 3 |
def item_clicked(self): item = self.list_widget.currentItem() self.label.setText("You have selected: " + str(item.text())) |
Run the code and this is the result.