On 16 Mrz., 21:56, Hugo Parente Lima <[email protected]> wrote:
> On Wednesday 16 March 2011 08:27:59 Markus Hubig wrote:
>
> > Now, what I wanna do is to update a progessbar every time I'm entering
> > a new State. The Problem is that I'm running thru my StateMachine but
> > the progessbar is updated only _after_ the QStateMachine is finished!
>
> > So what I'm missing here?
>
> You need to give Qt a chance to process the events, when you call
> machine.start() the machine starts, change states and until it finishes Qt
> doesn't process any event, so the GUI isn't updated.
>
> There's two possibilities to fix this:
>
> - Put your state machine code into a separated thread.

Thanks for this tip!

Now I've putted my QStateMachine into a separate thread, as you
suggested,
and it works ... mostly. Now if I start my application I get two error
messages:

1. QMetaObject::connectSlotsByName: No matching signal for
on_progress()
2. QObject::setParent: Cannot set parent, new parent is in a different
thread

Msg 1 appears when I start the application. Msg 2 when I press the
start Button.
But the application is running fine. While msg 1 is surely something
related to
the multi inheritance in my application but what's the course of Msg
2?

Hope someone has a tip for me ... ;-)

- Markus

--------------------8<----schnipp---------------------------------

import time
import threading
import platform

from PySide import QtCore
from PySide import QtGui

from ui_trimeibt import Ui_mainWindow

__version__ = '0.0.1'

class MainWindow(QtGui.QMainWindow, Ui_mainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self._progress = 0
        self._running = False
        self.startButton.clicked.connect(self._start_machine)
        self.on_progress.connect(self._update_progressbar)

    def _update_progressbar(self):
        self.progressBar_DUT1.setValue(self.progress)

    def _reset_progressbar(self):
        print "RESET"
        self.progressBar_DUT1.reset()

    def _state_machine(self):
        machine = QtCore.QStateMachine(self)
        machine.finished.connect(self._reset_progressbar)

        prepare = QtCore.QState()
        prepare.entered.connect(self.prepare_dut())
        flash = QtCore.QState()
        flash.entered.connect(self.flash_dut())
        config = QtCore.QState()
        config.entered.connect(self.config_dut())
        finish = QtCore.QFinalState()
        finish.entered.connect(self.finish_dut())

        prepare.addTransition(flash)
        flash.addTransition(config)
        config.addTransition(finish)

        machine.addState(prepare)
        machine.addState(flash)
        machine.addState(config)
        machine.addState(finish)
        machine.setInitialState(prepare)
        machine.start()

    def prepare_dut(self):
        print "State 1: Prepare DUT"
        self.progress = 20
        time.sleep(1)

    def flash_dut(self):
        print "State 2: Flash DUT"
        self.progress = 50
        time.sleep(1)

    def config_dut(self):
        print "State 3: Config DUT"
        self.progress = 70
        time.sleep(1)

    def finish_dut(self):
        print "State 4: DUT finished!"
        self.progress = 100
        time.sleep(1)

    @QtCore.Slot()
    def _start_machine(self):
        if not self._running:
            self._running = True
            thread = threading.Thread(target=self._state_machine)
            thread.start()

    def _get_progress(self):
        return self._progress

    def _set_progress(self, progress):
        self._progress = progress
        self.on_progress.emit()

    on_progress = QtCore.Signal()
    progress = QtCore.Property(int, _get_progress, _set_progress,
notify=on_progress)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    frame = MainWindow()
    frame.show()
    app.exec_()


--------------------8<----schnapp---------------------------------
_______________________________________________
PySide mailing list
[email protected]
http://lists.pyside.org/listinfo/pyside

Reply via email to