In this PyQt6 lesson we are going to learn about PyQt6 Creating QListWidget, we will learn how you can use coding and Qt Designer for creating QListWidget, using QListWidget we can add items in indexes.
This is the complete code for this lesson.
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 |
from PyQt6.QtWidgets import QApplication, QWidget,QLabel ,\ QVBoxLayout,QListWidget from PyQt6.QtGui import QIcon, QFont import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("PyQt6 ListWidget") self.setWindowIcon(QIcon("qt.png")) self.setGeometry(500,200, 500,400) # 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, "PyQt6") self.list_widget.insertItem(1, "wxPython") self.list_widget.insertItem(2, "Kivy") self.list_widget.insertItem(3, "TKinter") self.list_widget.insertItem(4, "Pyside2") self.list_widget.setStyleSheet('background-color:yellow') self.list_widget.clicked.connect(self.item_clicked) # create label self.label = QLabel("") self.setFont(QFont("Sanserif", 13)) self.setStyleSheet('color:brown') # 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()) |
This is our QVBoxLayout and we want to add our ListWidget in vertical layout.
1 |
vbox = QVBoxLayout() |
You can use QListWidget class for creating listwidget in PyQt6.
1 |
self.list_widget = QListWidget() |
You can use insertItem() method of the QListWidget for adding items, you need to specify the index of the item.
1 |
self.list_widget.insertItem(0, "PyQt6") |
In here we have connected the clicked signal of the listwidget with method that we have created.
1 |
self.list_widget.clicked.connect(self.item_clicked) |
This is our method, basically in this method we are going to get the current item from the listwidget and after that we set that in the label.
1 2 3 |
def item_clicked(self): item = self.list_widget.currentItem() self.label.setText("You have selected: " + str(item.text())) |
Run the complete code and this is the result
Now we want to use Qt Designer for creating QListWidget, we can just write pyqt5designer in our terminal, after opening the Qt Designer we need to create Widgets window, after that we add our widgets in Qt Designer.
- Add QListWidget with QLabel in the window
- Make the window layout vertically using QVBoxLayout
- Right click on ListWidget and add some items in QListWidget
Now convert your UI file to Py file using this command.
1 |
pyuic6 -x ListWidget.ui -o mylistwidget.py |
This is the converted file.
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 |
from PyQt6 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(374, 293) 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) font = QtGui.QFont() font.setPointSize(12) self.listWidget.setFont(font) 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) self.verticalLayout.addWidget(self.listWidget) # connected the clicked signal of the listwidgets self.listWidget.clicked.connect(self.item_clicked) self.label = QtWidgets.QLabel(Form) font = QtGui.QFont() font.setPointSize(14) font.setBold(True) font.setWeight(75) self.label.setFont(font) 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) #our new method that is connected with the 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", "PyQt6")) item = self.listWidget.item(1) item.setText(_translate("Form", "Kivy")) item = self.listWidget.item(2) item.setText(_translate("Form", "TKinter")) item = self.listWidget.item(3) item.setText(_translate("Form", "wxPython")) item = self.listWidget.item(4) item.setText(_translate("Form", "PySide2")) item = self.listWidget.item(5) item.setText(_translate("Form", "New Item")) 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()) |
There are two changes that we have brought in this code, first we have connected the clicked() signal of QListWidget with the method that we have created at the bottom of the code.
1 |
self.listWidget.clicked.connect(self.item_clicked) |
And this is our method, basically in this method we are going to get the current item from the listwidget and after that we set that in the 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.