1

I am developing a plugin for QGIS 2.8 which uses the data of the active layer. It has a dockable window. When the user changes the active layer, the contents of the plugin window's widgets must reflect to the event, without any additional user interaction. Please provide some ideas for a solution?

1 Answer 1

1

You have to use signals. Here you have a nice documentation http://doc.qt.io/qt-4.8/signalsandslots.html.

Every widget can send different signals. For example combobox (QComboBox object) send signal every time if you select another text inside:

activated(const QString & text) This signal is sent when the user chooses an item in the combobox. The item's text is passed. Note that this signal is sent even when the choice is not changed. If you need to know when the choice actually changes, use signal currentIndexChanged().

Now if you want to do some things when you interact with this widget, you have to connect wigdet with function.

connect(self.widget, QtCore.SIGNAL("activated(const QString&)"), self.function)

where:

widget = QComboBox()
def function():
    # do something

For more signal types check the documentation of particular qt widget.

2
  • Thank you! Your answer helped me to find the answer quickly. This tutorial link contains the precise answer: <br/># connect to the currentLayerChanged signal of QgsInterface <br/>result = QObject.connect(self.iface, SIGNAL("currentLayerChanged(QgsMapLayer *)"), self.handleLayerChange) Commented Apr 23, 2015 at 15:50
  • Please don't use the old connect method, it's pretty gross. Use new style self.widget.activated.connect(self.function)
    – Nathan W
    Commented Apr 24, 2015 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.