Hot answers tagged

18

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 QGIS3 on this question :Writing automated tests for QGIS plugins? And you will find an interesting OpenGis.ch's paper here about the migrations tools. What ...


6

I'm assuming when you say "PyQt5", you mean you are using QGIS 3.x. In which case: layer.pendingFields() becomes: layer.fields() And: layer = QgsMapLayerRegistry.instance().mapLayersByName('Layer_Name')[0] becomes: layer = QgsProject.instance().mapLayersByName('Layer_Name')[0] You can read here about the API changes and here for the PyQt4 and PyQt5 ...


4

You're missing the python3-pyqt5.qtsvg dependency. Install that and retry.


4

You need to connect your LIVSMILJO comboBox to a signal and return the current text of LIVSMILJO comboBox every time selection of text changes in LIVSMILJO comboBox.This can be done as follows def addValues(self): txt = self.dlg.LIVSMILJO.currentText() if txt=='1': self.dlg.KALKINNH.clear() self.dlg.KALKINNH.addItems(["hei","morna"]) ...


4

The environment and path needs to be set for Qt Designer to access the QGIS Custom widgets. These can be set by running the "qgis-designer.bat" file that comes with a QGIS installation. For a network installation, this file is located in: ...\OSGEO4W64\bin\qgis-designer.bat The contents of this batch file are: @echo off call "%~dp0\o4w_env.bat" call ...


4

In QGIS you can't use an application object, because qgis itself is a QtApplication. Try to execute lines 6-10 alone and you'll see a widget on the screen. To have a full functional dialog, you'll need to a have something like a qgis-plugin. To work with pyqt only, try an standalone python shell or an ide. For plugin creation, you use the qgis plugin "...


4

You can try layerList = QgsProject.instance().layerTreeRoot().findLayers() for layer in layerList: print(layer.name()) That will give you a list of layer names Check out the QgsLayerTree class for more info


4

With this documentation about PyQt5 signals, try this : self.buttonBox.accepted.connect(Configuration.accept) self.buttonBox.rejected.connect(Configuration.reject)


4

Test-it in another method like that : def run (self): self.dlg = Dialog() self.dlg.horizontalSlider.sliderMoved.connect(lambda v: self.adjust_sliders(1, v)) self.dlg.horizontalSlider_2.sliderMoved.connect(lambda v: self.adjust_sliders(2, v)) self.dlg.show() def adjust_sliders(self, slider_num, moved_value): if self.dlg.checkBox.isChecked(): ...


4

I don't see the practical side of creating classes inside other classes. Just split it. Below is how I will make your code work : import os from PyQt5 import uic from PyQt5.QtWidgets import QWidget from qgis.core import QgsProcessingAlgorithm os.chdir("/home/julien/Bureau/") FORM_CLASS, _ = uic.loadUiType("ui_file.ui") class MyWidget(QWidget, FORM_CLASS)...


3

In a plugin (displayed "Always On Top"), with PyQGIS 3, to avoid it jumps right to the end without calling the display_point function you need following changes in your code: . . . from PyQt5.QtCore import Qt from qgis.gui import QgsMapToolEmitPoint . . . def __init__(self, iface): """Constructor. . . . # Create the dialog (after ...


3

Perhaps it is due to some bug in QGIS 3.0 as it was the case with QGIS 2.18 see https://issues.qgis.org/issues/16743 one thing you can do to solve this issue is to install latest version QGIS 3.0.2 from https://qgis.org/en/site/forusers/download.html and hope that it was taken care of or you can report it as bug.


3

It is likely because each time the plugin is run, you are adding another connect signal to the function. You should disconnect this at the end of your loadDate(self) function using: self.dlg.toolButton.clicked.disconnect(self.select_output_file)


3

Solved by installing the library libqt5sql5-psql sudo apt install libqt5sql5-psql


3

In QGIS 3.2.0, custom widgets seem to be available: Tested on Windows 7 64-bit. Edit: I used the QGIS Standalone Installer Version 3.2 (64 bit) and following the simple on-screen instructions. Once installed, I simply ran Qt Designer with QGIS 3.2.0 custom widgets.exe which was found in the Start menu. I also tested QGIS Standalone Installer Version 3.2 (...


3

I found the solution. I wrote the python console like that: from PyQt.QtCore import * and it works!


3

No it's not possible yet. Plugins must be written in PyQt4.


3

In python you have "events" and "events handler". when an event occurs they usually (not always...) generate a signal that can be caught by the event handler. for example : curLayer.selectionChanged.connect(self.test) the event is selectionChanged and the event handler is the function handling the signal received (in that case the handler should be a ...


3

Just had a similar problem, a solution might be to add a layout and add the labels, lineedit and comboxes in to it, also make sure that they are large enough (that the minimum width is large enough etc.


3

To open and close layouts programmatically in QGIS 3.4, you could use something like this: Open a layout by name: layout_name = 'Test' layout = QgsProject().instance().layoutManager().layoutByName(layout_name) iface.openLayoutDesigner(layout) Then closing the layout is as simple as: iface.openLayoutDesigners()[0].close() To close a layout by its name: ...


3

Note that you are creating a new instance of your dialog class every time the run() method is called. The isVisible() method is always returning false because you are calling it on the new instance of your dialog before you ever call show() on that object. Why don't you declare self.dlg as an instance variable in the __init__() method. For example: class ...


3

You can customize the QGIS GUI via Settings --> Interface Customization... However, since you are looking for a Python solution: Iterate iface.fileToolBar().actions() Ask for each action's objectName() and compare to those you want to preserve And, finally, for those actions that don't match, use iface.fileToolBar().removeAction(action).


3

For anyone interested, here is an implementation of a CheckableComboBox. class CheckableComboBox(QComboBox): # Subclass Delegate to increase item height class Delegate(QStyledItemDelegate): def sizeHint(self, option, index): size = super().sizeHint(option, index) size.setHeight(20) return size def ...


2

Try something like that: try: # action for QGIS 3/PyQt5 except: # action for QGIS 2/PyQt4


2

QGIS is supposed to work with the OS-supplied PyQt5. In this case I was able to reproduce your error by installing PyQt5 from PyPI. I was able to confirm the same problem in the Ubuntu 18.04 LTS. Try removing the PyPI-supplied PyQt5 and seeing if QGIS 3 launches: sudo pip3 uninstall PyQt5 Note: You may still be unable to run QGIS 3. I have tried this on ...


2

You can remove it: import os os.remove("doc.pwg")


2

The relative path for a file in a plugin is: :/plugins/PluginClassName/filename If in doubt, search for the path used for the icon_path in the def initGui(self): method.


2

I have found an answer: con = ['name_of_db','C:\\path_to_db\\andorra.sqlite'] QSettings().setValue("SpatiaLite/connections/%s/sqlitepath" %(con[0]), con[1]) iface.reloadConnections() This code will create an 'visible' connection with the given sqlite database. Complete code: conn = sqlite3.connect(r'C:\\path_to_db\\andorra.sqlite') uri = ...


2

You can use the method addDockWidget from the QgisInterface: https://qgis.org/api/classQgisInterface.html#ad2c81fbbe55bc9609af7a36e56d6857f When you have your iface object, or from from qgis.utils import iface, you can do: iface.addDockWidget(Qt.RightDockWidgetArea, your_dock_object) to add your dock widget on the right area.


2

You could connect the pushButton to a function so that when it is clicked, it will do something: result_btn = self.dlg.pushButton.clicked.connect(self.someFunction)


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