PyQt5 Tutorials
About Lesson

In this PyQt5 lesson we want to learn about PyQt5 QCheckBox with Qt Designer,  a QCheckBox is an option button that can be switched on (checked) or off (unchecked). Checkboxes are typically used to represent features in an application that can be enabled or disabled without affecting others, but different types of behavior can be implemented. so there are two ways that you can create pyqt5 checkbox, the first way is to use Qt Designer and in the second way by using coding. so now first let’s do it using Qt Designer.

 

 

Open your Qt Designer, you can just write pyqt5designer in your terminal, after opening the Qt Designer you need to create Dialog without Buttons window. now we add widgets in Qt Designer.

  • First add a vertical or VBoxLayout
  • Add QLabel with QHBoxLayout in the vertical layout 
  • in the QHBoxLayout you need to add a label also a VBoxLayout, and in the VBoxLayout add three QCheckBox.
  • Also at the end you need to add another Label, it should be in your main QVBoxLayout.
CheckBox Design In PyQt5
CheckBox Design In PyQt5

 

 

After completing the design you need to save the .ui file, iam going to name it checkbox.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.

 

 

Now this is the code 

 

 

We have added some new codes in their so the first one is the stateChanged() signal for our QCheckBox.

 

 

And also the method or slot that we have connected with the stateChanged() signal of pyqt5 checkbox.

 

 

 

Run the complete code and this is the result.

PyQt5 How to Create CheckBox with Qt Designer
PyQt5 How to Create CheckBox with Qt Designer

 

Creating CheckBox with Coding 

OK now let’s create our checkbox using coding, and this is the complete source code for PyQt5 How to Create CheckBox with Qt Designer

 

 

basically in this example we want to use two layouts first one is a QHBoxLayout, in the HBoxLayout we want to add our three checkboxes.

 

 

You can use QCheckBox class for creating of the checkboxes, and you need to just add the title for the checkbox.

 

 

Set the icon for the checkbox, make sure that you have already added an icon to your working directory.

 

 

This is used for icon size of the checkbox.

 

 

In here we have connected the toggled() signal of the QCheckBox with the item_selected() method that we will create.

 

 

Also you need to add your checkbox in the HBoxLayout using this code.

 

 

And this is the method that we have already connected with toggled() signal of the checkbox. basically in this method we are going to check the value of the checkboxes.

 

 

Run the complete code and this is the result.

PyQt5 How to Create CheckBox with Qt Designer
PyQt5 How to Create CheckBox with Qt Designer