[Qt-qml] QML needs an Interface concept

Stephen Kelly steveire at gmail.com
Tue Jul 27 13:11:29 CEST 2010


Hi,

I solved the action tree problem I posted about before by implementing
a recursive element container.

http://lists.trolltech.com/pipermail/qt-qml/2010-July/000725.html

You can see it here currently:

http://websvn.kde.org/trunk/KDE/kdepim/mobile/lib/

Now I can do something like this:

  KPIM.ActionMenuContainer {
    x : 100
    y : 100
    width : 600
    height : 300
    id :myTree
    actionItemHeight : 70
    actionItemWidth : 200
    actionItemSpacing : 2

    KPIM.ActionList {
      name : "edit"
      KPIM.ActionListItem { name : "mark_message_important" }
      KPIM.ActionListItem { name : "copy" }
      KPIM.ActionListItem { name : "move" }

    KPIM.ActionList {
      name : "view"
      KPIM.ActionListItem { name : "mark_message_action_item" }
      KPIM.ActionListItem { name : "week" }
      KPIM.ActionListItem { name : "day" }
      KPIM.ActionList {
        name : "view"
        KPIM.ActionListItem { name : "month" }
        KPIM.ActionListItem { name : "week" }
        KPIM.ActionListItem { name : "day" }
      }
      KPIM.ActionListItem { name : "other" }
    }
  }

So a ActionItemContainer contains a list of Elements which can be
either of type ActionListItem or ActionList.

An ActionList can contain ActionListItems and other ActionLists.

Something that became apparent is that ActionListItem and ActionList
need to implement the same interface, but there is no way to enforce
that.

I thought it would be useful to be able to do something like define an
interface, and specify that certain elements implement that interface
and require that only elements which implement an interface can be
used in certain components.

For example,

in ActionInterface.qml

Interface {
  property string name
  property string iconName

  property bool enabled
  property bool toggled

  signal triggered(string name)
}

in ActionListItem.qml

Rectangle implements (ActionInterface) {

  property alias name : myText.text
  property alias iconName : myImage.source

  property bool enabled
  property bool toggled

  signal triggered(string name)

  Rectangle {
    Text {
      id : myText
    }

    Image {
      id : myImage
    }
  MouseArea {
    // The usual
  }
  }
}

and in ActionList.qml

Rectangle implements (ActionInterface) {

  property alias name : selfAction.name
  property alias iconName : selfAction.iconName

  property alias enabled : selfAction.enabled
  property alias toggled : selfAction.toggled

  signal triggered(string name)

  ActionListItem {
    id : selfAction

    onTriggered : {
      childItems.visible = true;
    }
  }

  ActionMenuContainer {
    id : childItems
  }

}

Or, for greater granularity, the interfaces Toggleable and Triggerable
could be defined separately and then we could do

ActionListItem.qml

Rectangle implements(Toggleable, Triggerable) {
 ...
}

or

ActionInterface implements (Toggleable, Triggerable) {

  // Additional to the inherited interfaces.
  property string name;

}

Or if it must not introduce new parsing syntax


ActionListItem.qml

Rectangle {
  implements : "Toggleable,Triggerable"
}


The ActionItemContainer would then need some way of specifying the
interface requirement such as

ActionItemContainer.qml

Rectangle {

 default property contents container.data

  Implements {
    target : contents
    interface : "ActionInterface"
  }

  Item {
    id : container
  }

}

For a comparable system see Zope.Interfaces.

http://apidoc.zope.org/++apidoc++/Book/ifaceschema/interface/show.html

What do you think? Interfaces for QML good thing bad thing, useful
thing or required thing?


All the best,

Steve.



More information about the Qt-qml mailing list