I'm trying to learn more about using QGIS Map Canvas widgets and creating custom map tools, so the information from the PyQGIS cookbook is very helpful. After trying to experiment with mywnd.py from this page: https://www.qgis.org/en/docs/pyqgis_developer_cookbook/canvas.html I had a few difficulties.

On first try running the script, I got ImportError: cannot import name QString. To overcome this error I imported like so:

    from PyQt4.QtCore import SIGNAL, Qt, QString
except ImportError:
    QString = str

That allowed me to get through the import. However, after inputting the following lines into the python console as directed by the cookbook, I got NameError: global name 'QString' is not defined

w = mywnd.MyWnd(qgis.utils.iface.activeLayer())

1 Answer 1


That documentation in the cookbook is a little out of date. You shouldn't need to use QString at all now, and the following code should work:

actionZoomIn = QAction("Zoom in", self)
actionZoomOut = QAction("Zoom out", self)
actionPan = QAction("Pan", self)

Basically, just remove all references to QString - they'll be converted automatically behind the scenes and you don't need to do it manually yourself anymore.

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.