Hot answers tagged

19 votes
Accepted

Make QGIS python plugin for both versions 2.x and 3.x?

Documentation Here you can find what is new and what is break under the PyQGIS API. To get details about how to port Python2 to Python3 go there You can find some detail about testing from QGIS2 to ...
Hugo Roussaffa's user avatar
15 votes
Accepted

KeyError: '-1' when adding field and calculating expression with PyQGIS

The error you get says the field index is -1, so your new field wasn't found in your attribute table. That may be happening because: You need to use QVariant.Double instead of QVariant.Float. You've ...
Germán Carrillo's user avatar
12 votes
Accepted

How to add a filter to “QFileDialog.getOpenFileName”

According to the documentation you can add filter in the definition of your class: QString QFileDialog.getOpenFileName (QWidget parent = None, QString caption = QString(), QString directory = ...
SIGIS's user avatar
  • 902
8 votes

No module named 'osgeo'

Try this, pip install --global-option=build_ext --global-option="-I/usr/include/gdal" GDAL==`gdal-config --version`
Saroj Rai's user avatar
  • 249
8 votes
Accepted

Select and use raster layer from python

Hopefully the following will help get you started, I've added comments to try and explain how it works. Essentially, you can use a QgsMapLayerComboBox to filter out all layers loaded in QGIS and ...
Joseph's user avatar
  • 75k
8 votes
Accepted

Plugin crashes QGIS 2.18 if running simuntaneously with python console

This is indeed an effect of using the print statement and having the python console open. And using the logging module as proposed is a very good approach to solving this issue. Here follow some ...
Matthias Kuhn's user avatar
7 votes
Accepted

Get the identified feature

This is a simple code for connect your QgsMapToolIdentifyFeature signal. from qgis.gui import QgsMapToolIdentifyFeature def onFeatureIdentified(feature): print "feature selected : "+ str(feature....
Fran Raga's user avatar
  • 7,780
6 votes
Accepted

No module named 'osgeo'

This sounds like you have multiple python installations, and GDAL is installed on one and not the other. Try the following (which I have based upon this). Create a text file within your python ...
JimT's user avatar
  • 2,363
6 votes

Developing QGIS plugin: AttributeError: object has no attribute

In new_mmqgis.py: You set self.first_start = True in initGui, then, you set self.first_start = False in run method. In geocode_web_service method, you are checking if self.first_start == True. That if ...
Kadir Şahbaz's user avatar
5 votes

How to add a filter to “QFileDialog.getOpenFileName”

dialog = QtGui.QFileDialog() fname = dialog.getOpenFileName(None, "Window name", "", "CSV files (*.csv)") Will do the job. Just pass None as 1st agrument.
dmh126's user avatar
  • 6,622
5 votes
Accepted

QgsMapCanvas has no attribute 'setLayers'?

The reason is, you are trying to use QGIS 3 API on QGIS 2. Here is the API for QGIS 2: https://qgis.org/api/2.18/classQgsMapCanvas.html Here is the API for QGIS 3: https://qgis.org/api/...
Matthias Kuhn's user avatar
5 votes
Accepted

QGIS2 to QGIS3 plugin migration issue AttributeError: type object 'QObject' has no attribute 'connect'

With this documentation about PyQt5 signals, try this : self.buttonBox.accepted.connect(Configuration.accept) self.buttonBox.rejected.connect(Configuration.reject)
J. Monticolo's user avatar
  • 15.6k
4 votes

error sip.setapi(api, 1) in pyQGIS

Import qgis.core before PyQt4.
Ben's user avatar
  • 727
4 votes

Plugin crashes QGIS 2.18 if running simuntaneously with python console

It looks like I overused print statement while debugging in python. Switching to logging module solved the problem. Use logging module instead of print, it's much more convenient and practical. It's a ...
Filip Zorić's user avatar
4 votes

Showing startup-messages on QGIS splashscreen

I figured out another solution (QGIS 3.4): startup.py from PyQt5.QtCore import QSettings,QStandardPaths from PyQt5.QtGui import QPixmap from PyQt5.QtWidgets import QLabel, QWidget, QSplashScreen,...
The Rabbit's user avatar
4 votes
Accepted

How to get QGIS canvas in plugin window?

In QT designer you need make right click on the widget and select "promote to", and write the class and header as seen in the image. after you need compile the .ui. In the class that loads this ui, ...
Fran Raga's user avatar
  • 7,780
3 votes
Accepted

Create a upload button in PyQt QGIS plugin

If the uploaded file is not in image or .csv file it should thrown an message. It is not the most common approach I think. The best way to do this is to create a file dialog, that allows to choose ...
dmh126's user avatar
  • 6,622
3 votes

How to use processing in a PyQT plugin in QGis

I can partly answer my own question! The following works now also in my Python plugin: The reason was that I had a differed order of xmin/xmax/ymin/ymax for PROJWIN in the plugin code. Thus the ...
geotom's user avatar
  • 61
3 votes

Check if QLineEdit is NULL on a custom form?

If I understand well, you want to check for NULLs when the custom value for NULL is changed by the user from the Settings. In this case, you may simply use: from qgis.core import * if <what you ...
mgri's user avatar
  • 16.1k
3 votes
Accepted

Rendering a custom bounding box to an image file using python and QGIS?

Your code is ok, but there is probably a typo in the p list. The first item value seems too big for the location you are working on (I generally work using CRS UTM 32N): probably the last digits of ...
mgri's user avatar
  • 16.1k
3 votes
Accepted

How to make my plugin dialog window open as QGIS default plugins?

Add parent=self.iface.mainWindow() parameter when instantiating dialog. def __init__(self, iface): self.iface = iface ... ... def run(self): ... # change dialog class name self....
Kadir Şahbaz's user avatar
2 votes

No module named 'osgeo'

Got the same problem. I fixed ir by installing python-gdal using synaptic with my Debian Jessie
Gustavo Cordoba's user avatar
2 votes

How to call a method when self.iface.actionSelectRectangle().trigger() is released?

An answer is here : How to define an event for features selected in mapcanvas in qgis using python You can connect your function to the selectionChanged signal of your vector layer Example code from ...
YoLecomte's user avatar
  • 2,865
2 votes
Accepted

Converting GeoTIFF file to numpy array to QPixmap with PyQt4?

I managed to do it in the end, so I'll post the answer here in case anyone needs it later. The problem was that my image contained float32 and sometimes even complex32 data, but QImage does not ...
neistuff's user avatar
2 votes
Accepted

Trigger plugin button using PyQGIS?

It seems that this is more a PyQt4 question. I guess the best way of doing it is to give your actions an objectName, so that you can access them directly instead of looping through toolbars and ...
Germán Carrillo's user avatar
2 votes
Accepted

QGIS plugin fields will not reset

You can call the function reset_form on closeEvent of the form. Add this code to GNATDialog class: def closeEvent(self, event): self.reset_form()
Chiller's user avatar
  • 226
2 votes

Keep track of current layers in PyQt plugin for QGIS

You can do that by using 'groupRelationsChanged' method of QgsLegendInterface to connect slot. Following code snippet is referred to pluging and works as expected: . . . def __init__(...
xunilk's user avatar
  • 29.5k
2 votes
Accepted

Keep track of current layers in PyQt plugin for QGIS

You can use QgsMapRegistry signals like layersAdded and LayersRemoved. Just connect function that can modify your QComboBox. Another solution is to use QgsMapLayerComboBox: https://qgis.org/api/...
dmh126's user avatar
  • 6,622
2 votes

Get the identified feature

To stick with your code : def onFeatureIdentified(self, ff): print("feature selected") def run(self): mCanvas = self.iface.mapCanvas() f = QgsMapToolIdentifyFeature(mCanvas, self.iface....
SIGIS's user avatar
  • 902

Only top scored, non community-wiki answers of a minimum length are eligible