If you wish to run imperative code from within QML as a result of some event then I think your approach is not that much correct.