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  property var intTransientLocalPublisher: Ros2.createPublisher("/intval_tl", "example_interfaces/msg/Int32", Ros2.QoS().transient_local())
6}

In order, the arguments are the topic, the type and the qos. For backward compatibility and convenience, you can also pass an integer which will set the history_policy of the QoS to keep_last and the depth to the integer value. If you only pass the topic and type, the default QoS will be used which are best effort, durability volatile and depth 1. See the Ros2 singleton for different preconfigured QoS profiles.

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 depth if the history policy of the QoS is set to keep_last. (readonly)

QoSWrapper qos

The Quality of Service settings of this publisher.

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)

class QoSWrapper

Wrapper to enable setting QoS settings in QML. Defaults to 1 depth, best effort reliability and volatile durability.

Public Functions

qml_ros2_plugin::QoSWrapper reliable()

Sets the reliability policy to reliable. Returns the QoSWrapper for chaining.

qml_ros2_plugin::QoSWrapper best_effort()

Sets the reliability policy to best effort. Returns the QoSWrapper for chaining.

qml_ros2_plugin::QoSWrapper durability_volatile()

Sets the durability policy to volatile. Returns the QoSWrapper for chaining.

qml_ros2_plugin::QoSWrapper transient_local()

Sets the durability policy to transient local. Returns the QoSWrapper for chaining.

qml_ros2_plugin::QoSWrapper keep_all()

Sets the history policy to keep all messages. Returns the QoSWrapper for chaining.

qml_ros2_plugin::QoSWrapper keep_last(int depth)

Sets the history policy to keep last messages. Returns the QoSWrapper for chaining.

Parameters:

depth – The number of messages to keep.

int depth() const

Returns the depth of the QoS policy. If history policy is set to keep_last, this is the number of messages to keep.