3

According to the latest official QGIS 3.X API reference there is a class that should insert attribute table of a layer. I tried to do it, but got an error.

Here is a piece of code I used:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from qgis.core import *
from qgis.gui import *

project = QgsProject.instance()
manager = project.layoutManager()
layout = QgsPrintLayout(project)
layout.initializeDefaults()
layout.setName('console')
manager.addLayout(layout)
itemMap = QgsLayoutItemMap.create(layout)
tbl = QgsLayoutItemAttributeTable.create(layout)
layer_attr =  QgsProject.instance().mapLayersByName(u'buildings')[0]
tbl.setVectorLayer(layer_attr)
layout.addLayoutItem(tbl)

And after the last line I get an error

TypeError: QgsLayout.addLayoutItem(): argument 1 has unexpected type 'QgsLayoutItemAttributeTable'

Can't understand why it is happening because QgsLayoutItemAttributeTable logically should be supported by layout methods. Main things in my code were taken from this question.

1 Answer 1

1

I suggest you to look at this thread: Creating and manipulating an HTML frame in QGIS 3.2.0 Print Composer using PyQGIS

I think its the same process for an attribute table. You have to add a QgsLayoutFrame with this commandline: layout.addLayoutItem()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.