QML scoping within StateMachine and State

420 views Asked by At

I read the doc about QML scoping.

By this doc the following is allowed (under Component Instance Hierarchy second example from the above doc):

My StateMachine (BaseStateMachine.qml):

import QtQuick 2.5
import QtQml.StateMachine 1.0 as DSM

DSM.StateMachine {
   property string someProperty

   running: true
}

My State (BaseState.qml):

import QtQuick 2.5
import QtQml.StateMachine 1.0 as DSM

DSM.State {
   onEntered: someProperty = "some value"
}

My Main (main.qml):

import QtQuick 2.5
import QtQml.StateMachine 1.0 as DSM

ApplicationWindow {

   // ...

   BaseStateMachine {

      initialState: state

      BaseState {
          id: state
      }
   }
}

But I get the following error: qrc:/qml/BaseState.qml:4: ReferenceError: someProperty is not defined

Am I misunderstanding something? I also read the doc about the StateMachine in qml and didn't find any exception for scoping within the StateMachines and States.

Update:

If I add an id to BaseStateMachine.qml like this:

import QtQuick 2.5
import QtQml.StateMachine 1.0 as DSM

DSM.StateMachine {
   id: _baseStateMachine

   property string someProperty

   running: true
}

then QtCreator becomes aware of the someProperty in BaseState.qml. Under "becomes aware of" I mean, that if I ctrl/command+click on the property in BaseState.qml it brings me to BaseStateMachine.qml. As soon as I remove the id from BaseStateMachine.qml the QtCreator can't find the someProperty anymore.

1

There are 1 answers

0
Kevin Krammer On

The difference to the second example in the documentation that you are referring to, is that in the example the "inner" elements are instantiated inside the "outer" element's definition file.

The equivalent in your setup would be using the BaseState type inside the BaseStateMachine.qml file.

I.e. this should work

import QtQml.StateMachine 1.0 as DSM

DSM.StateMachine {
    property string someProperty

    running: true
    initialState: state

    BaseState {
         id: state
    }
}