By J. Monticolo


2019-04-11 14:27:19 8 Comments

With PyQGIS, I want to set project layer properties (Project Menu > Properties > DataSource) such as Identifiable, Searchable and Required (for Read Only, the method is QgsVectorLayer.setReadOnly() (link)).

In the API doc, for the method QgsProject.nonIdentifiableLayers(), it says:

Deprecated since version since: QGIS 3.4 use QgsMapLayer.setFlags() instead

But there is no clear documentation (for me) or example for how to use this new method QgsMapLayer.setFlags().

I would like to understand how to use this method.

1 comments

@J. Monticolo 2019-04-11 14:27:19

I tried to understand the behavior of the setFlags method by see on a vector layer the results of :

vl = iface.activeLayer()
flags = 1
vl.setFlags(QgsMapLayer.LayerFlag(flags))
# result : the layer is identifiable and required

I looked at the Python API doc for QgsMapLayer, in these attributes and :

  • Identifiable = 1 (= 2 power 0)
  • Removable = 2 (= 2 power 1)
  • Searchable = 4 (= 2 power 2)

If we draw a binary table :

Num2 power 22 power 12 power 0
   0          0             0             0      
   1          0             0             1      
   2          0             1             0      
   3          0             1             1      
   4          1             0             0      
   5          1             0             1      
   6          1             1             0      
   7          1             1             1      

and a table of project layer settings (DataSource tab) with the result of vl.setFlags(QgsMapLayer.LayerFlag(Num)), 1 as True and 0 as False :

NumSearchable Required Identifiable
   0           0               1               0       
   1           0               1               1       
   2           0               0               0       
   3           0               0               1       
   4           1               1               0       
   5           1               1               1       
   6           1               0               0       
   7           1               0               1       

  • Identifiable behavior equals 2 power 0
  • Searchage behavior equals 2 power 2
  • but Removable behavior is the inverse of 2 power 1.

... and wait ! Required is the inverse of Removable.

For set these settings for a layer in a project, the code is :

vl = activeLayer()
identifiable = QgsMapLayer.Identifiable
searchable = QgsMapLayer.Searchable
removable = QgsMapLayer.Removable

# for a non-required and identifiable layer
vl.setFlags(QgsMapLayer.LayerFlag(identifiable + removable))

# for a searchable only layer
vl.setFlags(QgsMapLayer.LayerFlag(searchable + removable))

# for a searchable and required layer
vl.setFlags(QgsMapLayer.LayerFlag(searchable))

Or a function :

def layer_settings(vectorLayer, isIdentifiable=True,
    isSearchable=True, isRequired=False):

    flags = 0
    if isIdentifiable:
        flags += QgsMapLayer.Identifiable

    if isSearchable:
        flags += QgsMapLayer.Searchable

    if not isRequired:
        flags += QgsMapLayer.Removable

    vectorLayer.setFlags(QgsMapLayer.LayerFlag(flags))

vl = iface.activeLayer()
# for an identifiable and required layer
layer_settings(vl, True, False, True)

Related Questions

Sponsored Content

1 Answered Questions

1 Answered Questions

[SOLVED] Toggle labels on/off for a layer with PyQGIS 2?

  • 2018-12-12 08:33:59
  • Jochen Schwarze
  • 57 View
  • 1 Score
  • 1 Answer
  • Tags:   qgis pyqgis labeling

1 Answered Questions

[SOLVED] Writing a python processing script with Qgis 3.0

  • 2018-05-14 09:16:26
  • Kantan
  • 3412 View
  • 12 Score
  • 1 Answer
  • Tags:   qgis pyqgis qgis-3

1 Answered Questions

[SOLVED] How to change layer symbology in QGIS 3 with QgsFeatureRendererV2?

2 Answered Questions

[SOLVED] Understanding QGIS API documentation

  • 2018-04-17 08:11:27
  • Lennert
  • 470 View
  • 6 Score
  • 2 Answer
  • Tags:   pyqgis references

1 Answered Questions

[SOLVED] How to use the method makeDifference in the console?

1 Answered Questions

[SOLVED] How to set and configure a value relation edittype in QGIS 2.14?

  • 2016-07-30 18:35:32
  • joskal
  • 314 View
  • 3 Score
  • 1 Answer
  • Tags:   qgis pyqgis

1 Answered Questions

[SOLVED] pyQGIS - set grid annotation direction 'Vertical Descending'

  • 2016-07-07 10:37:28
  • florian
  • 63 View
  • 2 Score
  • 1 Answer
  • Tags:   pyqgis

1 Answered Questions

[SOLVED] Load postgis vectorlayer with AuthConfigId

1 Answered Questions

[SOLVED] How to write Python code with QList class

Sponsored Content