PyQt5 Tutorials
About Lesson

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.

 

 

Also you can do the same for the QLabel.

 

 

 

This is the design.

PyQt5 QListWidget Design
PyQt5 QListWidget 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.

 

 

And this is the converted code also we have added our clicked() signal with the method that we want to connect.

 

 

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.

 

 

 

Run the complete code and this is the result.

PyQt5 ListWidget - Working with QListWidget in PyQt
PyQt5 ListWidget – Working with QListWidget in PyQt

 

 

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.

 

 

These are our window requirements like window title, window icon, width and height of the window, x and y position of the window.

 

 

In here we have created the object of QListWidget.

 

 

Also you need to add some items in the QListWidget using insertItem() method.

 

 

This is the clicked() signal of the listwidget that we have already connected with item_clicked() method.

 

 

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.

 

 

 

 

Run the code and this is the result.

Working with QListWidget in PyQt
Working with QListWidget in PyQt