[PyQt] Example of populating a QML list from Python for use by other QML objects

Louis Simons lousimons at gmail.com
Wed Jan 4 18:11:11 GMT 2017


Is there an example of populating a QML list of objects from Python so that
other objects using the members of these lists receive updates when their
properties are changed?  In my app.qml (simplified psuedocode), I have
something like:

Item {
  Store { id: 'store' }

  ChannelsListing {
    channels: store.channels
  }

  ChannelsSummary {
    channels: store.channels
  }
}

A channel is a QML object looks like (and has an appropriate Python class
registered):

Channel {
  property int id
  property str name
}

The ChannelsListing and ChannelSummary are pure QML with no need for Python
(hopefully).  They simply display the data and update when the data
changes.  They don't change the channels themselves, but instead will send
an action upwards to whatever back-end the store is getting the data from
to modify the data.  This should ensure that all views of the data remain
consistent.

I can make a Python Store class derived from QQuickItem and register it to
the Store QML type:

class Store(QQuickItem):
  def __init__(self):
    self._channels = []

  @pyqtProperty(QQmlListProperty)
  def channels(self):
    return QQmlListProperty(Channel, self, self._channels)

The store also connects to an ZMQ socket, and receives model data for the
channels in JSON.  However, when I try to use the append functions of a
QQmlListProperty, I get errors as Store.channels is actually a
QQmlListPropertyWrapper, which I can "len" and iterate over, but can't
modify.

Is this approach possible?  Am I trying to put a square peg in a round
hole?  Should I be doing the data modification entirely in QML?  I'm open
to any suggestions and architecture recommendations.

Thanks,
Louis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20170104/c90fee05/attachment.html>


More information about the PyQt mailing list