2

I'm usig QGIS 2.18.6.

UPDATE2: I've mangaged to get (assign/find) the actual widget (value Map) for my attribute (field) "ibruk" with calls from the python console in QGIS:

>>>layer = qgis.utils.iface.activeLayer()
>>>index = layer.fieldNameIndex("ibruk")
>>>layer.editFormConfig().widgetType(index)
u'ValueMap'
>>>layer.editFormConfig().widgetConfig(index)
{u'': u'', u'I bruk': u'1', u'Ikke tatt i bruk (under bygging)': u'5', u'(opplagt) ute av bruk': u'3', u'Usikker': u'4'}

But, how do I now set a specific value (like setValue, changeValue, setText or similar) so this value automatically appear in the drop down list and the user may just click OK? (See further down for more explanation.)

--

UPDATE1: I'm trying to change the value in a Value map widget ("ibruk") in QGIS automatically using python-code in the Function Editor.

I have tried something similar described in Layer with custom UI not working properly, but I'm not able to change the value/text in the combo-box. I want "Usikker" (= value 4) to appear as chosen value in the drop box.

from qgis.core import *
from qgis.gui import *
from qgis.utils import iface
from PyQt4.QtCore import *
from PyQt4.QtGui import *

@qgsfunction(args='auto', group='Custom')
def ingen_adkomst(feature, parent):
    layer = qgis.utils.iface.activeLayer()
    index = layer.fieldNameIndex("ibruk")
    QgsEditorWidgetWrapper.fromWidget( ibruk ).setValue( 4 )
    """return 4"""

I'm having trouble to assign the Value Map (called "ibruk"), then change the value. I have found examples of how to create a QComboBox with PyQt and use setText etc. But in my case, I already have the Value Map (set in Layer Properties in QGIS). I just want a specific option to be chosen based on a constraint, see below. Is it possible?

--

I have one attribute "o1mrk" (Value Map) where the user may choose one of 5 options. If user choose 5 ("o1mrk" = 5), then I want another attribute's ("ibruk") value automatically to be set to 'Usikker' (value 4) saving the user some clicking and thereby some time.

So if "o1mrk"= 5 then "ibruk"='Usikker'.

First I tried this constraint for "ibruk":

CASE
  WHEN "o1mrk"=5 THEN "ibruk"=4
  ELSE "ibruk" IN (1,3,5)
END

But this requires the user to manually choose "ibruk" like 'Usikker' when "o1mrk" is set to 5 - and no less clicking. I want 'Usikker' to appear automatically when "o1mrk" = 5 (i.e. Ingen adkomst).

I have managed to partly achieve what I want by setting a constraint for "ibruk" and using Function Editor (which I'm not mastering, just trying my best by looking at questions and example-code here at gis.stackexchange.com and QGIS Expression):

CASE
  WHEN "o1mrk"=5 THEN ingen_adkomst()
  ELSE "ibruk" IN (1,3,5)
END

where ingen_adkomst() is my own Python function:

from qgis.core import *
from qgis.gui import *
from qgis.utils import iface
from PyQt4.QtCore import QVariant

@qgsfunction(args='auto', group='Custom')
def ingen_adkomst(feature, parent):
    layer = qgis.utils.iface.activeLayer()
    index = layer.fieldNameIndex("ibruk")
    f_id=feature.id()
    layer.changeAttributeValue(f_id,index,"Usikker")

The attribute value in the attribute table changes. However, the visible Value Map-option (value) is still not changed - preventing me from clicking OK.

  1. Originally before changing "o1mrk": enter image description here enter image description here

  2. After choosing "o1mrk" = 5 (Ingen adkomst). The value of "ibruk" is appearantly not changed (but the value is changed in the attribute table) and turns red: enter image description here enter image description here

  3. Even if I manually choose 'Usikker' from the drop down list, the option stays red: enter image description here

How may I change my constraint and/or code so that when "o1mrk" = 5 (Ingen adkomst), the value of "ibruk" changes to 'Usikker" also in the drop down list so the red colour disappear and I'm allowed to click OK?

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.