I have a QSlider that I want to set it's value programmatically overtime not just initially. The issue is that when I set the value of the slider after I move it, the slider position does not move to the correct value position, but the value does change.
This is the code to reproduce the issue (I am running this on an M1 Mac):
from PyQt5.QtWidgets import (QWidget, QSlider, QHBoxLayout,
QLabel, QApplication, QPushButton)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout()
sld = QSlider(Qt.Horizontal, self)
sld.setRange(0, 100)
sld.valueChanged.connect(self.updateLabel)
self.label = QLabel('0', self)
self.label.setAlignment(Qt.AlignCenter | Qt.AlignVCenter)
self.label.setMinimumWidth(80)
button = QPushButton('Move to 12', self)
button.pressed.connect(lambda: sld.setValue(12))
hbox.addWidget(sld)
hbox.addSpacing(15)
hbox.addWidget(self.label)
hbox.addSpacing(15)
hbox.addWidget(button)
self.setLayout(hbox)
self.setGeometry(300, 300, 350, 250)
self.setWindowTitle('QSlider')
self.show()
def updateLabel(self, value):
self.label.setText(str(value))
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()