0

Why slider and label do not display intermediate results sent by signal from Connections? Slider and label only display the final result. How to make it appear consistently?

test.py

    class Test(QObject):
        percent = pyqtSignal(int, arguments=['percent'])
    
        @pyqtSlot()
        def start(self):
            for i in range(101):
                self.percent.emit(i)
                print(i)
                time.sleep(0.1)
    
    if __name__ == '__main__':
        test = Test()
        app = QGuiApplication(sys.argv)
        engine = QQmlApplicationEngine()
        engine.rootContext().setContextProperty("Test", test)
        engine.load('test.qml')
        sys.exit(app.exec_())

This is test.qml

ApplicationWindow {
    visible: true
    width: 500
    height: 100
    RowLayout {
        Slider {
            id: slider
            implicitWidth: 300
            from: 1
            value: 1
            to: 100

        }
        Label {
            id: label
            width: 100
            text: "0"
        }
        Button {
            highlighted: true
            text: "Go"
            onClicked: {
                Test.start()
            }
        }
    }
    Connections {
        target: Test
        function onPercent(percent) {
            slider.value = percent
            label.text = percent
        }
    }
}

Slider and label only display the final result. How to make it appear consistently?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
voron.run
  • 47
  • 7
  • 1
    I think you should do this asynchronously, I mean the test should be run in a different thread so UI thread will be able to update the slider. – folibis Sep 28 '20 at 12:05

1 Answers1

1
class Test(QObject):
    percent = pyqtSignal(int, arguments=['percent'])

    @pyqtSlot()
    def start(self):
        t1 = threading.Thread(target=self.threadTest)
        t1.start()

    def threadTest(self):
        for i in range(101):
            time.sleep(0.1)
            self.percent.emit(i)


if __name__ == '__main__':
    test = Test()
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    engine.rootContext().setContextProperty("Test", test)
    engine.load('test.qml')
    sys.exit(app.exec_())

it works.

voron.run
  • 47
  • 7