2

I would like to edit the variables of a composer item in QGIS 2.18.x. The widget itself is working so far. In my example, I am selecting a QgsComposerLabel called "Plankopf" to show its variables.

What is not working so far is:

  • saving the edits: I thought that changes done throught the QgsVariableEditorWidget would be saved but that's not the case. What do I have to change to get added/updated variables saved?

  • showing changed Variables in the Composer-Window: Even if I change variables with QgsExpressionContextUtils.setComposerItemVariable(), the changes are not visible in the Composer window. How can I refresh the variables widget in the Composer window?

    # -*- coding:utf-8 -*-
    from qgis.core import QgsExpressionContextUtils, QgsExpressionContext
    from qgis.gui import QgsVariableEditorWidget
    from qgis.utils import iface
    from PyQt4.QtGui import QInputDialog, QWidget, QFormLayout, QPushButton, QLineEdit
    from PyQt4 import QtCore
    from PyQt4.QtCore import QPoint
    
    class inputdialogdemo(QWidget):
        def __init__(self, parent = None):
           super(inputdialogdemo, self).__init__(parent)
    
        def getItem(self):
           items = []
           composerId = 0
           composers = iface.activeComposers()
           for item in composers:
               items.append( item.composerWindow().windowTitle() )
           item, ok = QInputDialog.getItem(self, u'Auswahl', 
             u'Druckzusammenstellung wählen', items, 0, False)
           if ok and item:
               return item
    
     ex = inputdialogdemo().getItem()
     print ex
    
     composerId = 0
     composers = iface.activeComposers()
     for item in composers:
         if item.composerWindow().windowTitle() == ex:
             break
         composerId += 1
    
    target_citem = NULL
    def itemsuche(name):
        for citems in composers[composerId].composition().items():
            if type(citems).__name__=='QgsComposerLabel':
                if citems.displayName() == name:
                    return citems
                    break
    
    
    target = itemsuche('Plankopf')
    
    canvas = iface.mapCanvas()
    variable_editor_widget = QgsVariableEditorWidget()
    expression_context = QgsExpressionContext()
    
    
    expression_context.appendScope(QgsExpressionContextUtils.composerItemScope(target))
    
    variable_editor_widget.setContext(expression_context)
    
    variable_editor_widget.reloadContext()
    variable_editor_widget.setEditableScopeIndex(0)
    print(variable_editor_widget.context())
    print(variable_editor_widget.editableScope())
    print(variable_editor_widget.settingGroup())
    print(variable_editor_widget.variablesInActiveScope())
    
    mapcanvas_upperleft = canvas.mapToGlobal(QPoint(0, 0))
    
    
    
    offset_x = 0
    offset_y = 30
    pos_x = mapcanvas_upperleft.x() + offset_x
    
    pos_y = mapcanvas_upperleft.y() + offset_y
    variable_editor_widget.setGeometry(pos_x, pos_y, canvas.width()/3, canvas.height()*0.75)
    
    
    variable_editor_widget.show()
    print variable_editor_widget.pos().x()
    print variable_editor_widget.pos().y()
    target.setHtmlState(0)
    target.setHtmlState(2)
    
  • You could try connecting the scopeChanged() signal to the reloadContext() slot – firefly-orange Aug 18 '18 at 11:42

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.