5

Let's consider this code snippet:

ApplicationWindow
{
    /**/
    states: State {}
    /**/
}

When running the application, I get

Cannot assign to non-existent property "states"

When using

ApplicationWindow
{
    /**/
    Item { states: State {} }
    /**/
}

There is no error. Why can't I use states inside an ApplicationWindow?

marmistrz
  • 5,296
  • 9
  • 31
  • 84

2 Answers2

9

I've just realized that ApplicationWindow inherits Window inherits QQuickWindow. Only types which inherit from Item have the states property.

The workaround is to use StateGroup.

Feel free to add a better solution ;)

BaCaRoZzo
  • 7,022
  • 6
  • 44
  • 72
marmistrz
  • 5,296
  • 9
  • 31
  • 84
  • 3
    Thanks, this works! Unfortunately, doing this will break states support in Qt Designer; it doesn't recognise the `StateGroup`. Also, noting that `ApplicationWindow` is the default root element in Qt Quick projects created using the New Project assistant, and official Qt tutorials suggest using root element states to build the interface (using Qt Designer), this is a confusion that many users will be bumping up against. – Daniel Saner Jan 08 '16 at 19:58
0

I was able to code States within an QML ApplicationWindow using Qt v5.11, with two caveats. One was that the Qt Creator v4.6.1 editor flagged SignalTransition with a warning indicating a State cannot have a child item. Despite that warning, the code from the example at http://doc.qt.io/qt-5/qmlstatemachine.html#a-simple-state-machine builds and runs correctly. The second caveat was that the Qt Creator editor ignored the major.minor version of import QtQml.StateMachine. Any numbers I typed in instead of 1.11 were accepted:

import QtQuick 2.11
import QtQuick.Controls 1.4
import QtQml.StateMachine 1.11

ApplicationWindow {
    ...