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
35.7k5 gold badges77 silver badges151 bronze badges
asked Mar 7, 2019 at 12:01

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

MrXsquared
36.2k22 gold badges76 silver badges127 bronze badges
answered Apr 24, 2020 at 11:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.