2

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.

closed as off-topic by PolyGeo Jun 20 '18 at 13:23

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

3

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)
2

QObject does have the connect function - without seeing the rest of your script, it's tough to know for sure, but it looks like you need to set the import for PyQt5 as opposed to PyQt4, per the update notes to the QGIS3.0 migration page. This dependency change is in addition to the shift from Python 2 to 3, so both issues have to be considered.

Native Python 2.7 (which I use still) has a Tools\Scripts folder that includes a 2to3.py script that corrects version 2 to 3 migration issues. According to the page linked above, you should have a similar script that includes updates to all QGIS script changes: path/to/QGIS/scripts/2to3

Try the following import statements at the top of your plugin code, or simply change all your current imports to PyQt5 manually:

from PyQt5.QtCore import *
  • 1
    To import PyQt whatever the version, you can do 'from qgis.PyQt.QtCore import X'. No need to update yoru code when QGIS will use Qt6 for instance. – etrimaille May 9 '18 at 3:15

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