2

My c++-plugin for QGIS 2.18 should read out the labeling properties from the layer. So I was searching the API for getting informed of rule-based-labeling but still get more confused. In my example I have four rules. For every rule I want to read out the QgsPalLayerSettings and the properties of the rule (name, scale dependency, filter...)

enter image description here

For getting the QgsPalLayerSettings a ProviderID is needed. In the api I found, the member ruleKey() of a Rule is the Unique rule identifier (for identification of rule within labeling, used as provider ID) But there also is a list of providers in the labeling object of the layer. So I ran through the layer with the following code:

    if (vLyr->labeling()->type()=="rule-based")
    {
        const QgsRuleBasedLabeling* rl = static_cast<const QgsRuleBasedLabeling*>( vLyr->labeling() );
        QgsRuleBasedLabeling::Rule* rootRule = rl->rootRule()->clone();

        qDebug() << QString("Root Rule [%1] ").arg(rootRule->ruleKey());
        QStringList sp;
        rootRule->subProviderIds(sp);

        int i=0;
        foreach(QgsRuleBasedLabeling::Rule* rl, rootRule->children())
        {
            qDebug() << QString("Rule [%1] %2").arg(rl->ruleKey()).arg(sp.at(i));
        }

        foreach(QString sProv , vLyr->labeling()->subProviders())
        {
            qDebug() << QString("Provider [%1] ").arg(sProv);
            QgsPalLayerSettings layerSettings = vLyr->labeling()->settings( vLyr, sProv );
        }
    }

And this is the output:

"Root Rule [{0149ca5f-1ae3-46c8-a245-16e55bb9ce3c}] " 
"Rule [{ede1075f-0c72-461f-be71-22987a6ab011}] {ede1075f-0c72-461f-be71-22987a6ab011}"  
"Rule [{559d4578-770b-49c4-a7cb-eb0665e90838}] {559d4578-770b-49c4-a7cb-eb0665e90838}" 
"Rule [{63523eeb-b29d-4225-a4c8-5d8936c031f0}] {63523eeb-b29d-4225-a4c8-5d8936c031f0}" 
"Rule [{3c5edfb3-a717-48da-bc83-a4969ee549cc}] {3c5edfb3-a717-48da-bc83-a4969ee549cc}" 
"Provider [{72fefc10-2f5c-424c-8524-6c2cc7db7e26}] " 
"Provider [{67b3a0c7-c2eb-47a9-bab9-9c3fd7905bbc}] " 
"Provider [{48c4d4da-d94b-435f-8424-fe6e1c545ab8}] " 
"Provider [{b59c0c89-98fe-42b6-99d3-4b5a4efc63ae}] " 

I supposed that the four keys/providerIDs are the same but now I'm confused and don't know what to read out. I don't understand the difference between subProviders() and rooleKeys. And because this class is not available in python, there are no examples to find.

EDIT:

I found out the layersettings I get from rulekeys are empty, those from subProviders have the right informations. But how to combine them? Tried with QgsRuleBasedLabeling::Rule::subProviderIds but the IDs are the same like ruleKeys.

1 Answer 1

4

Rule keys and provider IDs are equivalent. The issue in your code is that you clone the root rule (rl->rootRule()->clone()) and so the new tree of rules gets new rule keys (=provider IDs).

I would not worry about labeling providers (and their IDs) though - better to stick with QgsRuleBasedLabeling::Rule objects that are easy to traverse and query. To get a labeling settings for a particular rule, it is just a matter of calling rule->settings() that either returns pointer to internal QgsPalLayerSettings object or it returns null pointer if no labeling settings are associated with the paricular rule.

1
  • Thats it, The 'clone()' I just copied without a reason from my 'setting-rulebasedlabeling-code'. Thank you a lot! Think I'd never seen this in my organisational blindness :-) Commented Sep 15, 2017 at 6:40

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.