Access non geometry layer for Plugin

Hi,

I’m attempting to access a non-geometrical layer to enumerate all values within a specific field. While I can retrieve QgsVectorLayer instances using mapCanvas.mapSettings.layers, I’ve noted that, as specified in the QField source code

non-spatial layers are automatically excluded from this list.

Additionally, I’ve observed that dashBoard.activeLayer provides access to the currently active layer. However, my requirement is to access any layer within the project, not just the active one.

Is there a method to access non-geometrical layers within QField’s QML environment?

Below my qml to test layer acces :

import QtQuick

Item {
  id: plugin

  property var mapCanvas: iface.findItemByObjectName('mapCanvas')

  Component.onCompleted: {

    let layers = mapCanvas.mapSettings.layers;
    let concatenatedLayerNames = layers.map(layer => layer.name).join(", ");
    iface.logMessage(`Layer Names: ${concatenatedLayerNames}`);

    let firstLayer = layers[0];
    iface.logMessage(`Layer Name: ${firstLayer.name}`);
    iface.logMessage(`Fields Name: ${firstLayer.fields.names}`);

    let fieldName = 'PLTM_PARC';
    iface.logMessage(`Index of ${fieldName} is ${firstLayer.fields.indexOf(fieldName)}`);
    
  }
}

Imported from GitHub discussion by @paul-carteron on 2024-12-04T14:32:04Z

paul-carteron , have you tried this function: QGIS API Documentation: QgsProject Class Reference ? In the qml plugin environment you can access the current project via the qgisProject object.

So provided you know your layer name you could do qgisProject.mapLayersByName(“my layer”).


Imported from GitHub comment by @nirvn on 2024-12-04T14:52:11Z

Pro tip: in the QGIS documentation, you can see properties accessible in QML within the properties section, and you can see which functions are exposed by spotting the Q_INVOKABLE tags.


Imported from GitHub comment by @nirvn on 2024-12-04T14:53:18Z

nirvn It works perfectly, thank you!

I wasn’t aware that qgisProject is accessible in QField plugins. What I take away after watching your presentation, I that i can use in Qfield Plugin :

You - obviously - already know all this, but I’m writing more with the goal of documenting and centralizing ressources. With that in mind, I couldn’t find where it’s explicitly documented that qgisProject exposes the QgsProject class. Do you know where I can find where this is defined?


Imported from GitHub comment by @paul-carteron on 2024-12-04T15:50:27Z

paul-carteron , you’d see it exposed in the QML context here: https://github.com/opengisch/QField/blob/f3c1fb9b351bba39ed7771efb486e898a11432e5/src/core/qgismobileapp.cpp#L575

We’d most welcome documentation additions once you feel happy about your gathering of resources.


Imported from GitHub comment by @nirvn on 2024-12-04T15:55:18Z

Thanks, I’ll keep that in mind for the documentation suggestion !


Imported from GitHub comment by @paul-carteron on 2024-12-04T16:12:23Z