1

I would like to use the richer data and styling API QgsComposerTextTableV2 offers. However I get this error when adding it to the composer:

TypeError: QGraphicsScene.addItem(QGraphicsItem): argument 1 has unexpected type 'QgsComposerTextTableV2'

here is the code:

    r = iface.mapCanvas().mapRenderer()
    c = QgsComposition(r)
    rows = self.buildRows() # A list of lists
    composerTable = QgsComposerTextTableV2(c, False)
    composerTable.setContents(rows)
    c.addItem(composerTable)
    return c

could someone provide code demonstrating a minimal implementation ?

1 Answer 1

1

Composer tables are a special type of item... a "multi frame" item. They require QgsComposerFrame items to actually display their content. It's done this way as a single table can have its contents shown over multiple frames (eg on different pages). Here's some code which should work:

# grab first composition for now
c=iface.activeComposers()[0].composition()

# create a text table
table = QgsComposerTextTableV2( c, False )

# add it to the composition
c.addMultiFrame(table)

# create 2 frame items to display contents of table in
frame1 = QgsComposerFrame( c, table, 5, 5, 100, 30 )
frame1.setFrameEnabled( True )
frame2 = QgsComposerFrame( c, table, 5, 40, 100, 30 )
frame2.setFrameEnabled( True )

# add the frames to the composition. These are the items which actually
# show the contents of the table
table.addFrame( frame1 )
c.addItem( frame1 )
table.addFrame( frame2 )
c.addItem( frame2 )

# setup the table's columns.
# *Important* QGIS will crash if col1 and col2 go out of scope. You may need
# to make them global
col1 = QgsComposerTableColumn('col 1')
col2 = QgsComposerTableColumn('another col' )
table.setColumns([col1,col2])

# set some sample content text
table.setContents([['row 1','text'],['row2','more text']])
0

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.