PyQt5 Tutorials
About Lesson

In this Python PyQt5 lesson we want to learn about Python PyQt5 QSlider , using QSlider widget you can create vertical or horizontal slider, and  it lets the user move a slider handle along a horizontal or vertical groove and translates the handle’s position into an integer value within the legal range.

 

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 QHBoxLayout 
  • In the HBoxLayout add a QSlider with QLabel 

 

 

Also you can right click and Change Stylesheet.

 

 

 

This is the design.

QSlider Qt Designer
QSlider Qt Designer

 

 

 

This is the ui file for the slider.

 

 

OK now we want to load our ui file, so for this copy the ui file and paste to your directory, after that create a new python file, iam going to call it LoadSlider.py and add this code, for loading the ui file we want to use uic module from pyqt5.

 

 

You can see in the above code we have used loadUi() method for loading the ui file.

 

 

In our main design we have a QSlider with a QLabel, so we need to find these widgets in the ui file, as we have done in the above code, we can use findChild() method for finding the widgets in ui file.

 

 

Also we have connected our valueChanged() signal of slider with the changed_slider() method.

 

 

And this is the method that we have already connected with the signal, in this method we are going to just get the text from the slider and we set the text in the label using setText() method.

 

 

 

Run the complete code and this is the result.

PyQt5 Slider - Creating QSlider in PyQt
PyQt5 Slider – Creating QSlider in PyQt

 

 

Creating PyQt QSlider with Coding 

OK now we want to create our pyqt5 slider  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.

 

 

So you can use QSlider class object for creating slider in Python PyQt5.

 

 

 

And this is the method that we have already connected with the signal, in this method we are going to just get the text from the slider and we set the text in the label using setText() method.

 

 

 

Run the complete code and this is the result.

Creating QSlider in PyQt
Creating QSlider in PyQt