CoffeeScript in qml, proof of concept.



  • In case anyone's interested, I hacked together a fork of qtdeclarative (based on dev) that allows coffeescript to be used in QML:

    https://github.com/corngood/qtdeclarative.git

    I've just been using it in a small hobby project, so I'm not making any promises about how well it works.

    You can make a standalone script file with a .coffee extension, and add:

    @.pragma language "coffeescript"@

    Then use:

    @import "test.coffee" as Test@

    In qml, for property assignments and signal handlers, instead of using a colon, you can put a ->

    @
    Loader {
    source -> if someFlag then "a.qml" else "b.qml"
    Keys.onPressed ->
    if event.key is Qt.Key_F1
    doSomething()
    event.accepted = true
    }
    @

    Everything after the -> will be parsed as a block of coffeescript until it finds a line that is equally or less indented than the line on which -> occurred. That's kind of dirty, but since coffeescript cares about indentation it doesn't bother me too much. Standard qml syntax will still be interpreted as javascript, and it's probably best to use it for ids/literals.

    Something to be aware of is that coffeescript will not let you assign a qml global (e.g. property on current object) with "property = value", and will instead declare a local 'var property'. To work around this you'll need to assign an id to the object and use "id.property = value".

    Things I've been thinking about for future work:

    1. hooks for qml/js translation in qtdeclarative, and coffeescript support in a plugin
    2. writing of translated qml/js (e.g. for build time translation)
    3. a pure coffeescript dialect of qml, using coffeescript's object notation
    4. modified coffeescript compiler that understands qml globals
    5. sourcemap support for debugging


  • nice.



  • Hm, that's an interesting development! Not that it makes much difference to me personally, I would really like to see proper language, like Python, instead of JS :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.