Javascript constants not allowed in models



  • I tried to create a numeric keyboard and stuck at the following: I want to let every button handle its own key, so I pass this key through the model:
    @Repeater {
    model: ListModel {
    id: model
    ListElement { label: "7"; value: "7"; key: Qt.Key_7 }
    ...@

    In the button component (delegate) I use this:
    @Keys.onPressed: {
    if (event.key == model.key) {
    event.accepted = true;
    clicked(model.value);
    ...@

    But the point is that it doesn't let me use Qt.Key* constants:
    @file:///D:/Projects/qmlinterface/Controls/Numpad.qml:25:43: ListElement: cannot use script for property value
    ListElement { label: "7"; value: "7"; key: Qt.Key_7 }
    ^@

    I could hardcode key codes as integers, but it's not really portable... What would you suggest, guys?



  • While this doesn't give you any immediate help, I'd suggest filing a bug report at http://bugreports.qt.nokia.com -- the limitation is meant to prohibit bindings in ListElement, and in this case Qt.Key_7 is being incorrectly interpreted as a binding rather than a constant.

    Regards,
    Michael



  • Filled a report. Thanks.
    Also got the following workaround suggestion in the qml mailing list from Gregory Schlomoff:

    @
    function keys(c) {
    var table = {key7: Qt.Key_7}
    return table[c];
    }

    Repeater {
    model: ListModel {
    ListElement { label: "7"; value: "7"; key: "key7" }
    }
    delegate: Text {
    text: model.label + " : " + keys(model.key)
    }
    }
    @

    seems to be clean and little overhead.


Log in to reply
 

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