On Tuesday, 11 December 2012 04:23:51 CEST, Alan Alpert wrote:
> For finer-grained control over QML it would make sense to have an API
> allowing import restrictions. Two usecases come to mind, platform
> security contexts and scriptable applications.

I'm currently using QML as a configuration language (being declarative looked 
like a great fit) in a random application. The end goal was to have one file 
per supported "type of thing" that the application works on. The description of 
each thing looks like this:

  import foo.bar 1.0

  ThingType {
    // ...
    SubThing {
      // ...
    }
  }

Because I'm stuck on 4.x for this particular application, I had to work with 
what is available now. In the end, I settled on:

- Using just a QDeclarativeEngine as a good start -- no need for UI items in my 
use case

- The ThingType and SubThing are actually implemented in C++

- Enforcing that the top-level item created in QML is exactly the ThingType. 
This can be done when instantiating the component by checking its type via 
qobject_cast<QMLMyTypeThing*> and refusing to work with that QML file if this 
cast fails

- The ThingType has a QDeclarativeListProperty<QMLMySubThing> Q_PROPERTY which 
is also a default property. This way, the type of the children of the ThingType 
is enforced.

- If you need further enforcing of certain rules ("each ThingType has to be 
yellow"), you can inherit from QDeclarativeParserStatus and implement 
componentComplete. My code works like this:

  if (!m_color.isYellow())
    qmlInfo(this) << "The color must be yellow."

- Because these errors are only warnings (and it looks like you cannot really 
trigger a fatal error from this stage), I have an isOk() method which I call 
before I use the data structure which gets created as a result of these QML 
files.

If it was possible to prevent defining the QtQuick namespace from being 
available, that would solve half of my problems (with the required property 
validation being the remaining half).

The blurb about qmlInfo() was in particular rather hard to discover -- I had 
read the docs about it before but could not recall the name of the function 
when I needed it. Searching for it via phrases like "qml error reporting" did 
not help much.

With kind regards,
Jan
_______________________________________________
Development mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/development

Reply via email to