Closing the Plugin: Sending Signals

Instead of using the button in the PluginToolbar to turn the plugin on and off, we now rely on the map click to open the plugin, and I've added an OK button to the plugin component to close it. This creates a simple example for creating and using custom signals.

Setting Up the Signal from the Component in d2_plugin_component.qml

Just as you would do in PyQt, our signal is defined within our component item

Rectangle {
  id: pluginFrame
  signal closed()
}

We then add a button to pluginFrame that emits the signal when clicked

Rectangle {
  id: pluginFrame
  signal closed()
  Button {
    id: closeButton
    onClicked: {
        closed()
    }
  }
}

Connecting the Signal in the Plugin

In demo2_select.qml, we added a Connections item to connect the signal from pluginFrame. This is the 3rd different method for handling signals and slots in QML.

Item {
  id: plugin
  Loader{
    id:pluginLoader
  }
  Connections {
    target: pluginLoader.item
    function onClosed(){
      pluginLoader.active = false
    }
  }
}

This corresponds to PyQt

 pluginLoader.item.closed.connect(onClosed)
 def onClosed(){
    pluginLoader.active=false
 }

Note here that the connection between slot and function follows a mandatory naming convention, where if the signal name is closed, the slot name must be onClosed. Hey. I don't make the rules.

I could tell you about layouts, but I think you'll be very bored if I do. So let's skip to demonstration 4 and I'll show you how to add and update a feature.