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 ...


13

This is a bug in NNJoin plugin. Open NNJoin_gui.py file located in "/Users/joanlee/Library/Application Support/QGIS/QGIS3/profiles/default/python/plugins/NNJoin/" using an editor. Add from qgis.PyQt.QtWidgets import QMessageBox line to imports section. Then save the file and (you may need to) restart QGIS.


8

Review this script. It creates a new QMainWindow and moves the existing map canvas to that window. I've made some changes. Firstly, add a file path (to QgsVectorLayer) to be displayed in map canvas. from PyQt5 import QtCore, QtGui, QtWidgets from qgis.gui import QgsMapCanvas from qgis.core import QgsVectorLayer class Ui_MainWindow(object): def setupUi(...


6

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


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 ...


6

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


6

Yes, there is a way. It's the customVariablesChanged signal from QgsProject. def my_slot(): print("A project variable was just changed!") QgsProject.instance().customVariablesChanged.connect(my_slot)


6

Change super(self, QgsMapTool).__init__(self, canvas) to super(QgsMapTool, self).__init__(canvas)


5

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 functions you define, by default. Thus, try in that way: self.canvas.scaleChanged.connect(self.showScale) self.canvas.xyCoordinates.connect(self.showXY) Edit: ...


5

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


5

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 ...


5

You can add the combobox to a dialog window. After closing the dialog, print prints the selected items. from qgis.PyQt import QtGui from qgis.core import * planet_list = ["Venus", "Earth", "Mars", "Jupiter", "Pluto"] items = QgsCheckableComboBox() items.addItems(planet_list) dlg = QDialog() layout = ...


5

I don't know how to do that in Qt Designer, but in PyQGIS directly, it is an easiest way : my_dlg = QDialog() my_dlg.resize(500, 300) my_dlg_lyt = QVBoxLayout(my_dlg) my_msg_bar = QgsMessageBar(my_dlg) my_dlg.show() pb = QPushButton("test") my_dlg_lyt.insertWidget(0, my_msg_bar) my_msg_bar.pushMessage("It works", "I'm so happy ...


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 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 (...


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

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

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 = ...


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

You should import QMessageBox from PyQt from qgis.PyQt.QtWidgets import QMessageBox


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

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


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

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 ...


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