Publish and Subscribe Example
The Publish and Subscribe example shows how to use the Qt Publish and Subscribe API to publish and retrieve values from the platforms Publish and Subscribe system.
This example program can act as either a publisher of Value Space content, or a subscriber of Value Space content, or both. It can also optionally act as the Value Space Manager. The behavior is selected by passing a combination of three optional command line parameters when starting the program. If no parameters are supplied the program defaults to acting as both a publisher and a subscriber.
Parameter | Description |
---|---|
-publisher | Publish values in the Value Space and create a dialog to control those values directly. |
-subscriber | Create a dialog to view Value Space items. |
Publisher
When acting as a publisher of Value Space content the program creates a PublisherDialog. This dialog contains a widget for setting the base path of the QValueSpacePublisher and widgets for setting the value of the three fixed attributes 'intValue', 'stringValue' and 'byteArrayValue'.
[Missing image publish-subscribe-publisher.png]
We declare the slots which we will use to respond to user input in the dialog.
And connect them to signals from the widgets in the constructor.
To publish values in the Value Space we first need to create a QValueSpacePublisher. This is done in the createNewObject()
slot, which is also used to handle changes in the base path. After the object is constructed we publish the initial values by explicitly calling the other slots.
We use QValueSpacePublisher::setValue() to set the values in response to dialog input.
As a final step in the constructor we explicitly call the createNewObject()
slot to create an initial QValueSpacePublisher.
Subscriber
When acting as a subscriber of Value Space content the program creates a SubscriberDialog. This dialog contains a widget for setting the base path of the QValueSpaceSubscriber and a table for displaying all of the values directly under base path in the Value Space.
[Missing image publish-subscribe-subscriber.png]
We declare two slots to propagate values from the Value Space to the dialog in response to change notifications from QValueSpaceSubscriber.
To read values from the Value Space we first create a QValueSpaceSubscriber and connect to the contentsChanged()
signal.
We explicitly call our subscriberChanged()
slot to populate the table with the initial values.
In response to the contentsChanged()
signal we update all the values in the table.