Hot answers tagged

14 votes

QGIS plugin: Problems importing resources (resources_rc) file - plugin doesn't load - PATH problems?

You can avoid having to edit the .ui file or compiling, etc by doing the following: import sys sys.path.append(os.path.dirname(__file__)) FORM_CLASS, _ = uic.loadUiType(os.path.join( os.path....
Jason Hight's user avatar
12 votes
Accepted

QComboBox multiple selection - PyQT5

For anyone interested, here is an implementation of a CheckableComboBox. class CheckableComboBox(QComboBox): # Subclass Delegate to increase item height class Delegate(QStyledItemDelegate): ...
Yoann Quenach de Quivillic's user avatar
11 votes
Accepted

how do I create simple yes/no dialog in qgis python plugin?

Use QMessageBox.question: from PyQt4 import QtGui reply = QMessageBox.question(self.iface.mainWindow(), 'Continue?', 'Your message here', QMessageBox.Yes, QMessageBox.No) if reply ==...
Maxim Dubinin's user avatar
11 votes

QGIS plugin: Problems importing resources (resources_rc) file - plugin doesn't load - PATH problems?

I finally fixed it after 3 hours of trying. As someone said QT Designer adds <resources> <include location="resources.qrc"/> </resources> to your UI file. I simply deleted those ...
Student Michal Wloga's user avatar
11 votes
Accepted

Adding a Tiles/wms? (openStreetMap) layer with python in QGIS

Tiles/WMS sources give you raster data. So, you have to use QgsRasterLayer() instead of QgsVectorLayer(). Try in this way: ... rlayer = QgsRasterLayer(urlWithParams, 'OpenStreetMap', 'wms') # ...
Kadir Şahbaz's user avatar
9 votes
Accepted

Work around for “Couldn’t load PyQt” error with QGIS 3.0.3 on Ubuntu 18.04?

I was able to reproduce this error by installing PyQt5 using the Python package manager. If you remove the Python-managed PyQt5 and instead install the OS version, QGIS should launch. I have confirmed ...
Lee Hachadoorian's user avatar
9 votes
Accepted

How to fix 'Could not find the Qt platform plugin “Windows” in “”' when implementing qgis in custom Python3 application?

Add the following environment variables at the beginning of your python script before loading qgis.core: #modify environment variables to find qgis and qt plugins during qgis.core import os.environ['...
Matt Brauer's user avatar
8 votes
Accepted

Do I have to publish own QGIS plugins?

Do you have to publish it? Legally, The GPL License of PyQGIS forces you to share the code only with your "client", therefore your company. So No, you don't have to publish it. You can keep them ...
Alexandre Neto's user avatar
8 votes
Accepted

Opening Plugin with another Plugin

For my example, I took the QGIS plugin Plugin Builder 3 (link). After install of this plugin, a new repository is made in : \QGIS\QGIS3\profiles\default\python\plugins\pluginbuilder3 (here is the ...
J. Monticolo's user avatar
  • 15.6k
7 votes
Accepted

PyQGIS Contour Labelling

Perhaps you will have more success working directly with the QgsPalLayerSettings class. The snippet below works for me on a contour layer generated with gdal_contour. vlayer = iface.activeLayer() ...
Ben W's user avatar
  • 20.6k
6 votes
Accepted

How to sort layers within groups with Python?

Sorting layers is a particularly painful task in PyQGIS, because of the lack of suitable methods in the QgsLayerTreeNode class. Based on your code, I managed to write a console script that seems to ...
ArMoraer's user avatar
  • 5,589
6 votes

How to use Threads in PyQgis, mainly to keep UI active?

Use processing in QGIS 3.x In QGIS 3.x there is an infrastructure in place to have things running in threads. Processing is a good candidate to be what you are looking for when doing "heavy ...
Matthias Kuhn's user avatar
6 votes
Accepted

Capturing mouse movement on QgsMapCanvas?

QgsMapTool You probably want to implement a QgsMapTool and set this as the active map tool. This offers several methods which you can implement to react canvasMoveEvent canvasPressEvent ...
Matthias Kuhn's user avatar
6 votes
Accepted

Keep UI Settings for a QGIS plugin

Right, you need to use QSettings to store values across QGIS sessions. QSettings is multi-platform, so you don't need to worry about where it stores the data on Linux, on Windows, or on Mac, it ...
Germán Carrillo's user avatar
6 votes
Accepted

How to show only postgres vector layers in a QgsMapLayerComboBox?

In QGIS 2.x you'd need to pass a list of layers not coming from PostgreSQL: excepted = [] for layer in QgsMapLayerRegistry.instance().mapLayers().values(): if hasattr(layer, 'providerType') and ...
Germán Carrillo's user avatar
6 votes

Usage of QgsTask and QgsTaskManager

There's a typo in that script - it should be isCanceled() (one l, not two) You should also add your task to the global task manager instead of creating a new manager: QgsApplication.taskManager()....
ndawson's user avatar
  • 27.4k
6 votes

How to use QT QGis custom widget in a plugin?

To properly use QT QGis custom widgets: Use imports such as: from PyQt4.QtGui import QDialog from qgis.gui import QgsFileWidget You must also edit the xx_dialog_base.ui file, to ensure that the ...
Bruno von Paris's user avatar
6 votes
Accepted

Connecting Qt GUI to python code in QGIS Plugin

Your "check boxes" don't work because you're putting your lines in the wrong place (run method). However, you need first to declare objects in __init__ method for connecting them to a 'change_status' ...
xunilk's user avatar
  • 29.7k
6 votes
Accepted

Making a Feature Form for a standalone PyQGIS application as in QGIS

The following script generates a simple form. You can use it in your standalone PyQGIS application to display attributes for a given feature. You can also specify fields to be displayed (or not to be ...
Kadir Şahbaz's user avatar
6 votes
Accepted

Capturing CTRL+Key In KeyPressEvent in PyQGIS QgsMaptool

QKeyEvent has a method modifiers(). You should use it. def keyPressEvent(self, event): if event.modifiers() & Qt.ControlModifier: if event.key() == Qt.Key_R: print("...
Kadir Şahbaz's user avatar
6 votes
Accepted

Display a progressBar and use the execution time of several functions in QGIS plugin

Add a variable to the first function to store starting time. For example (self.start). Define a new function to set progress. Then use that function instead of setValue in other functions called by ...
Kadir Şahbaz's user avatar
6 votes
Accepted

Connecting signals to different functions based on combobox selection

Instead of making individual connections for each combobox options, it is more reasonable to connect the button click event to one function which checks the combobox value and calls the related ...
Kadir Şahbaz's user avatar
5 votes
Accepted

Find out if checkbox checked - pyqgis

You can simply write something like: if self.dlg.myCheckBox.isChecked(): doSomething() else: doSomethingElse() Note that if you want something more dynamic (ie perform an action everytime the ...
ArMoraer's user avatar
  • 5,589
5 votes

Properter to get travel time as cost for network analysis

Here's a short Properter which I'm working on that might provide part of the answer. It aims to modify shortest path calculations for a road network by excluding freeways (because I'm using it for ...
Steve's user avatar
  • 361
5 votes

PyQGIS plugin doesn't work layer list combo box

Don't make your own - PyQGIS has a whole library of standard reusable widgets like this. In this case use QgsMapLayerComboBox. All the hard work is done for you, it's stable and unit tested, and will ...
ndawson's user avatar
  • 27.4k
5 votes
Accepted

Get xy coordinates of QgsMapCanvas in PyQGIS 3?

You don't need to define lambda function and to specify self.canvas.scale() and self.canvas.xyCoordinates(). Because scaleChanged event passes scale and xyCoordinates passes coordinates to the related ...
Kadir Şahbaz's user avatar
5 votes
Accepted

How to add DockWidget above the Layer panel using PyQGIS

You can re-add the Layers Panel which would place it below any existing panels: from qgis.PyQt.QtCore import Qt from qgis.PyQt.QtWidgets import QApplication from PyQt4.QtGui import QDockWidget iface....
Joseph's user avatar
  • 75.3k
5 votes
Accepted

PyQt5 QGIS Plugin - "native Qt signal is not callable" error

The problem is how you are trying to interact with the signal. Instead of checking for a signal with a conditional 'if:' statement, you should listen for it and connect it to a slot. the basic idea (...
Ben W's user avatar
  • 20.6k
5 votes
Accepted

Using PyQt in PyQGIS inside QGIS 3.16

I think the original code have a different indentation : import sys from PyQt5 import QtWidgets def window(): app = QtWidgets.QApplication(sys.argv) w = QtWidgets.QWidget() b = QtWidgets....
J. Monticolo's user avatar
  • 15.6k
5 votes
Accepted

PyQT plugin window is always smaller than when run in QGIS

Simply dragging and dropping widgets onto a dialog in Qt Designer is a quick way to get a working ui, but it does not in any way guarantee consistent appearance. To demonstrate this, I created a dummy ...
Ben W's user avatar
  • 20.6k

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