Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Javascript constants not allowed in models
Qt 6.11 is out! See what's new in the release blog

Javascript constants not allowed in models

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 3.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    leonty
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mbrasser
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leonty
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved