1

I'm trying to build a python plugin that utilizes the pyqgis class "QgsAttributeDialog". I've only found one website (aside from the pyqgis doc page) that shows an example of how to implement the script correctly, and this is the example I based my own script on.

https://qgis.org/api/classQgsAttributeDialog.html

(example script) https://webgeodatavore.github.io/pyqgis-samples/gui-group/QgsAttributeDialog.html

from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication, QObject, SIGNAL, QTimer
from PyQt4.QtGui import QAction, QIcon, QMessageBox, QColor, QDialogButtonBox
from qgis.gui import QgsMapTool, QgsMapToolEmitPoint, QgsMapTip, QgsAttributeDialog
from qgis.core import (
QGis,
QgsCoordinateTransform,
QgsCoordinateReferenceSystem,
QgsRectangle,
QgsMapLayer,
QgsExpression,
QgsFeatureRequest,
QgsFeature,
QgsVectorFileWriter,
QgsGeometry,
QgsVectorLayer,
QgsRasterLayer,
QgsMapLayerRegistry,
QgsPoint)    


def run(self):
    global layers
    layers = self.iface.legendInterface().layers()
    self.pointEmitter = QgsMapToolEmitPoint(self.iface.mapCanvas())
    QObject.connect(self.pointEmitter, SIGNAL("canvasClicked(const QgsPoint, Qt::MouseButton)"), self.selectNow)
    self.iface.mapCanvas().setMapTool(self.pointEmitter)

def selectNow(self, point, button):
    selectedLayer.removeSelection()
    self.iface.mapCanvas().refresh()
    radius = .00001
    rect = QgsRectangle(point.x() - radius, point.y() - radius,
    point.x() + radius, point.y() + radius)
    selectedLayer.select(rect, False)
    selected_features = selectedLayer.selectedFeatures()
    features = selected_features[0]
    attribute_dialog = QgsAttributeDialog(selectedLayer, features, True)
    attribute_dialog_from_iface = self.iface.getFeatureForm(selectedLayer, features)
    attribute_dialog.show()
    attribute_dialog_from_iface.show()

The issue is in the two "attribute_dialog.show()" and "attribute_dialog_from_iface.show()" lines in the script above (my plugin script). Currently the script allows me to click on a layer of road segments and selects whatever segment I've clicked on. That works just fine, and I have the selectNow function clear the selection each time I click (so it will only select one feature at a time). I then took the example script and changed the code slightly so that it takes the selected layer and QgsFeature's attributes and inputs it into the Attribute dialog box. However when I integrated the QgsAttributeDialog lines from the example, it opens the dialog box (with attribute data visible) for 1-2 seconds and then immediately causes my session in QGIS to crash.

I cannot find anything wrong with the input (the layer of interest, QgsFeature object, and boolean value). I was hoping that someone on stack exchange has used this class before and can point me in the right direction/assist with troubleshooting.

My QGIS version is 2.14.2.

  • I think there is something missing - for example, selectedLayer never gets initialized, although you are hiding global variables (like layers, which is global too) from us? – Henhuy Nov 1 '16 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.