0

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.

Silex
  • 2,583
  • 3
  • 35
  • 59
  • I think, as `State` does not inherit `QObject` or anything else that would provide reference to a parent, the usual way to resolve the scope does not work and you need to provide it with actual ids. If you give the `BaseStateMachine` an id, and reference this ID in the `State` it seems to work. – derM - not here for BOT dreams Nov 29 '16 at 15:40
  • Thanks for your answer @derM. I am doing something similar currently. The funny thing, that if you add an id to the state machine, someProperty actually becomes known for the IDE in the state...weird behaviour. – Silex Nov 29 '16 at 16:12
  • Weired indeed. Thanks for this interesting insight! – derM - not here for BOT dreams Nov 29 '16 at 18:21

1 Answers1

0

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
    }
}
Kevin Krammer
  • 5,159
  • 2
  • 9
  • 22