5

I am trying to develop a plugin for QGIS 3. For me it is quite the first time really working with pyqgis or python, so there might be something simple I am missing.

I want to use data defined override button within this plugin. If it is checked, the used value should be taken from this button (expression or attribute) instead of a spinbox. This is my layout with the button:

enter image description here

Unfortunately I could not find any documentation or related questions, which explain how to actually use this QgsPropertyOverrideButton. So I am only able to grab the value from the spinbox. This is my relevant code snipped:

    #Check where to gather attributes from: GUI or Layer?
    if self.dlg.Isochrones_WalkSpeed_Override.isActive() == True:
        WalkSpeed = self.dlg.Isochrones_WalkSpeed_Override.value() #Receiving Value from GUI: DataDefinedOverride
    else:            
        WalkSpeed = self.dlg.Isochrones_WalkSpeed.value() #Receiving Value from GUI: SpinBox

How do I grab the value from there either via an attribute from layer or via a given expression within edit field?

enter image description here

2
  • 1
    You have the official documentation, if it's checked emit this qgis.org/api/… and maybe the qgis test too test_qgspropertyoverridebutton.py
    – Fran Raga
    Commented Feb 8, 2020 at 20:32
  • Thx, figured out I have to use .isActive() to check if the button is active, but still struggling to figure out how to grab the value.
    – MrXsquared
    Commented Feb 8, 2020 at 21:14

1 Answer 1

3

I think, you should use toProperty() method, which returns QgsProperty. Then you can use value() or valueAs***() methods of QgsProperty to get value of defined expression. Or you can get raw expression string with expressionString() method. See docs for QgsProperty.

EDIT

value() and valueAs***() returns tuple where first element is actual value and second element is bool reffering to successfulness of conversion. So, it is possible to use python tuple unpacking feature like so:

val, success = property.value(context)
if success:
    doSomenthingWith(val)

So in your case you would need to write as example:

WalkSpeed, success = self.dlg.Isochrones_WalkSpeed_Override.toProperty().value(QgsExpressionContext())
1
  • 1
    Acording to PyQgis documentation, value() returns Tuple[Any, bool], which is exactly what you get. Firs element is actual value, second element contains information about succes of conversion. It corespond to 'ok' pointer parameter in C++ function call. So dont hasitate to use returned tuple ;)
    – DavidP
    Commented Feb 12, 2020 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.