Saturday, 4 June 2016

Converting QGIS 2 plugin to QGIS 3?



A while ago I made a plugin for QGIS for myself.


This worked until version 2.18. Now I have version 3.0 and would like to use this plugin here too. But I get some error messages. I have been able to solve most of them fairly easily. Only I can not do this. Perhaps there is someone among you who can help me in this.


This is the code that I used before.


 def initGui(self):
"""Create menu entries and toolbar icons inside the QGIS GUI."""

icon_path = ":/plugins/BFVWtools/"


"""Create the menu entrie Configuratie and toolbar icon inside the QGIS GUI."""
self.add_action(icon_path + "/icon.png", text=self.tr(u'Configuratie'),
callback=self.run, parent=self.iface.mainWindow())
self.action = QAction( QIcon( os.path.dirname(os.path.realpath(__file__)) + "/icon.png" ), "Configuratie",
self.iface.mainWindow() )
QObject.connect(self.action, SIGNAL("triggered()"), self.run)

The error message I get is the following


AttributeError: type object 'QObject' has no attribute 'connect' 

Traceback (most recent call last):
File "C:/OSGEO4~1/apps/qgis/./python\qgis\utils.py", line 345, in startPlugin
plugins[packageName].initGui()
File "C:/Users/Automatisering/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\BFVWtools\bfvw_tools.py", line 212, in initGui
QObject.connect(self.action, SIGNAL("triggered()"), self.run)
AttributeError: type object 'QObject' has no attribute 'connect'

Apparently there is no attribute 'Connect' in QObject




After your explanation, I have converted everything to PyQt5 and changed the connect to slot. The strange thing is that I now get all the entrys from the menu one after the other.




Answer



You are using the old style of connect. There is this blogpost explaining the old/new style of connect, which was already recommended in Qt4: https://robertbasic.com/blog/connecting-signals-and-slots-with-pyqt-the-new-style/


Try this:


your_object.name_of_signal.connect(your_function_slot)

So your code would be like:


self.action.triggered.connect(self.run)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...