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.

Taras
  • 32,823
  • 4
  • 66
  • 137
Pavel Pereverzev
  • 1,754
  • 12
  • 26

1 Answers1

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()

MrXsquared
  • 34,292
  • 21
  • 67
  • 117
Kinad
  • 81
  • 6