PyQt QSlider & QDial
QSlider provides sliders in a horizontal or vertical direction. A slider is a widget that adjusts values within a limited range.
Use the setTickInterval() method to adjust the spacing of the ticks on the slider, and the setTickPosition() method to adjust the position of the tick.
The input of the setTickInterval() method means a value, not a pixel. The inputs and functions of the setTickPosition() method are shown in the table below. (For example, setTickPosition (QSlider.NoTicks) or setTickPosition(0))
Related course: Create Desktop Apps with Python PyQt5
QSlider & QDial.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QSlider, QDial, QPushButton
from PyQt5.QtCore import Qt
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.slider = QSlider(Qt.Horizontal, self)
self.slider.move(30, 30)
self.slider.setRange(0, 50)
self.slider.setSingleStep(2)
self.dial = QDial(self)
self.dial.move(30, 50)
self.dial.setRange(0, 50)
btn = QPushButton('Default', self)
btn.move(35, 160)
self.slider.valueChanged.connect(self.dial.setValue)
self.dial.valueChanged.connect(self.slider.setValue)
btn.clicked.connect(self.button_clicked)
self.setWindowTitle('QSlider and QDial')
self.setGeometry(300, 300, 400, 200)
self.show()
def button_clicked(self):
self.slider.setValue(0)
self.dial.setValue(0)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
I created a slider, a dial widget, and a push button.
The sliders and dials are interconnected and always have the same value, and pressing the push button will make both widgets have a value of 0.
Description
self.slider = QSlider(Qt.Horizontal, self)
I created one QSlider widget. Set the horizontal or vertical direction by entering Qt.Horizontal or Qt.Vertical.
self.slider.setRange(0, 50)
self.slider.setSingleStep(2)
Set the value range from 0 to 50 with the setRange() method. The setSingleStep() method set the minimum units that are adjustable.
self.dial = QDial(self)
You have created one QDeal widget.
self.dial.setRange(0, 50)
Like the slider, it is scoped by the setRange() method.
self.slider.valueChanged.connect(self.dial.setValue)
self.dial.valueChanged.connect(self.slider.setValue)
The signals that occur when the slider and the value of the dial change are connected to each other in a method (setValue) that adjusts the value of the dial and slider, so that the values of the two widgets always match.
btn.clicked.connect(self.button_clicked)
When you click the 'Default' push button, connect the signal that button_clicked to the method.
def button_clicked(self):
self.slider.setValue(0)
self.dial.setValue(0)
button_clicked() # method adjusts both the slider and dial values to 0.
Therefore, if you click the 'Default' push button, the value of the slider and dial is reset to 0.
Related course: Create Desktop Apps with Python PyQt5