Publishers

A Publisher is used to publish messages on a given topic for delivery to subscribers.

Simple Example

Contrary to Subscribers, a Publisher can not be instantiated but is created using a factory method of the Ros2 singleton.

1/* ... */
2ApplicationWindow {
3  property var intPublisher: Ros2.advertise("/intval", "example_interfaces/msg/Int32", 10)
4  /* ... */
5}

In order, the arguments are the topic, the type and the queueSize (defaults to 1). Additional QoS options are currently not supported.

To publish a message using our Publisher, we can simply use the intPublisher variable defined earlier.

1SpinBox {
2  id: numberInput
3}
4
5Button {
6  onClicked: {
7    intPublisher.publish({ data: numberInput.value })
8  }
9}

where we pass an object with a data field containing the (integer) number of the SpinBox. This is according to the example_interfaces/msg/Int32 message definition.

API

Publisher

class Publisher : public QObjectRos2

Public Functions

unsigned int getSubscriptionCount()
Returns:

The number of subscribers currently connected to this Publisher.

bool publish(const QVariantMap &msg)

Sends a message to subscribers currently connected to this Publisher.

Parameters:

msg – The message that is published.

Returns:

True if the message was sent successfully, false otherwise.

Signals

void advertised()

Fired once this Publisher was advertised. This is either done at construction or immediately after ROS is initialized. Since this is only fired once, you should check if the Publisher is already advertised using the isAdvertised property.

Properties

QString type

The type of the published messages, e.g., geometry_msgs/Pose. (readonly)

QString topic

The topic this Publisher publishes messages on. This property is only valid if the publisher is already advertised! (readonly)

quint32 queueSize

The queue size of this Publisher. This is the maximum number of messages that are queued for delivery to subscribers at a time. (readonly)

bool isAdvertised

Whether or not this publisher has advertised its existence on its topic. Reasons for not being advertised include ROS not being initialized yet. (readonly)