Discussion:
[Qgis-developer] Add multiple frames to a composer attribute table using PyQGIS
James Stott
2015-11-01 09:09:00 UTC
Permalink
I have an attribute table that is around two hundred rows of data (but only
two columns wide). I would like to add an attribute table to a composer
with this data, and to do this I need to add multiple frames so I can add
the whole attribute table to one page. Doing this manually in the composer
I just add 5 frames side by side to one composer page, and I manage to get
all my data on one page.

I cannot figure out how to add an attribute table with multiple frames to a
composer using PyQGIS. I am problably missing something really obvious, but
I am stuck here.

When I try to define a QgsComposerFrame, I am told that I must specify the
QgsComposerMultiFrame it belongs to:

QgsComposerFrame
<http://qgis.org/api/classQgsComposerFrame.html#ab645cf6db9a1285a6983e05541f2d28f>
(QgsComposition <http://qgis.org/api/classQgsComposition.html> *c,
QgsComposerMultiFrame
<http://qgis.org/api/classQgsComposerMultiFrame.html> *mf,
qreal x <http://doc.qt.io/qt-4.8/qgraphicsitem.html#x>, qreal y
<http://doc.qt.io/qt-4.8/qgraphicsitem.html#y>, qreal width, qreal height)

How do I create the QgsComposerMultiFrame. It is an abstract class, I am
told that it is an abstract class and cannot be instantiated. If I try

multiFrame = QgsComposerMultiFrame(myCompositionTable, False)

I get the following error when I run my code.

qgis._core.QgsComposerMultiFrame represents a C++ abstract class and cannot
be instantiated

I cant find any documentation or examples on how to do this. So if someone
could shed some light on this it would be most appreciated. I am happy to
write up a bit of documentation once I have managed to do this.

Many thanks,

James
Nyall Dawson
2015-11-01 09:23:43 UTC
Permalink
Post by James Stott
I have an attribute table that is around two hundred rows of data (but only
two columns wide). I would like to add an attribute table to a composer with
this data, and to do this I need to add multiple frames so I can add the
whole attribute table to one page. Doing this manually in the composer I
just add 5 frames side by side to one composer page, and I manage to get all
my data on one page.
I cannot figure out how to add an attribute table with multiple frames to a
composer using PyQGIS. I am problably missing something really obvious, but
I am stuck here.
When I try to define a QgsComposerFrame, I am told that I must specify the
QgsComposerFrame (QgsComposition *c, QgsComposerMultiFrame *mf, qreal x,
qreal y, qreal width, qreal height)
How do I create the QgsComposerMultiFrame. It is an abstract class, I am
told that it is an abstract class and cannot be instantiated. If I try
multiFrame = QgsComposerMultiFrame(myCompositionTable, False)
I get the following error when I run my code.
qgis._core.QgsComposerMultiFrame represents a C++ abstract class and cannot
be instantiated
You create a QgsComposerAttributeTableV2 instead, which derives from
QgsComposerMultiFrame. Here's some c++ code which does this, which
should be pretty straightforward to translate to Python:


mComposerAttributeTable = new QgsComposerAttributeTableV2(
mComposition, false );
mComposition->addMultiFrame( mComposerAttributeTable );

mFrame1 = new QgsComposerFrame( mComposition, mComposerAttributeTable,
5, 5, 100, 30 );
mFrame2 = new QgsComposerFrame( mComposition, mComposerAttributeTable,
5, 40, 100, 30 );

mComposerAttributeTable->addFrame( mFrame1 );
mComposerAttributeTable->addFrame( mFrame2 );

mComposition->addComposerTableFrame( mComposerAttributeTable, mFrame1 );
mComposition->addComposerTableFrame( mComposerAttributeTable, mFrame2 );

mComposerAttributeTable->setVectorLayer( mVectorLayer );
mComposerAttributeTable->setDisplayOnlyVisibleFeatures( false );
mComposerAttributeTable->setMaximumNumberOfFeatures( 10 );
mComposerAttributeTable->setContentFont( QgsFontUtils::getStandardTestFont() );
mComposerAttributeTable->setHeaderFont( QgsFontUtils::getStandardTestFont() );
mComposerAttributeTable->setBackgroundColor( Qt::yellow );

When in doubt for something like this and you can't find any examples
online, a good last resort to check is in the QGIS unit tests:
https://github.com/qgis/QGIS/tree/master/tests/src
(or in this case
https://github.com/qgis/QGIS/blob/master/tests/src/core/testqgscomposertablev2.cpp)

They are filled with minimal test cases such as this which are useful
for seeing how various classes are intended to be used.

Nyall
Post by James Stott
I cant find any documentation or examples on how to do this. So if someone
could shed some light on this it would be most appreciated. I am happy to
write up a bit of documentation once I have managed to do this.
Many thanks,
James
_______________________________________________
Qgis-developer mailing list
http://lists.osgeo.org/mailman/listinfo/qgis-developer
James Stott
2015-11-03 09:31:02 UTC
Permalink
Hi Nyall,

Thanks very much for the advice. It is working now.

That was an excellent tip about checking the unit tests.

James
Post by James Stott
Post by James Stott
I have an attribute table that is around two hundred rows of data (but
only
Post by James Stott
two columns wide). I would like to add an attribute table to a composer
with
Post by James Stott
this data, and to do this I need to add multiple frames so I can add the
whole attribute table to one page. Doing this manually in the composer I
just add 5 frames side by side to one composer page, and I manage to get
all
Post by James Stott
my data on one page.
I cannot figure out how to add an attribute table with multiple frames
to a
Post by James Stott
composer using PyQGIS. I am problably missing something really obvious,
but
Post by James Stott
I am stuck here.
When I try to define a QgsComposerFrame, I am told that I must specify
the
Post by James Stott
QgsComposerFrame (QgsComposition *c, QgsComposerMultiFrame *mf, qreal x,
qreal y, qreal width, qreal height)
How do I create the QgsComposerMultiFrame. It is an abstract class, I am
told that it is an abstract class and cannot be instantiated. If I try
multiFrame = QgsComposerMultiFrame(myCompositionTable, False)
I get the following error when I run my code.
qgis._core.QgsComposerMultiFrame represents a C++ abstract class and
cannot
Post by James Stott
be instantiated
You create a QgsComposerAttributeTableV2 instead, which derives from
QgsComposerMultiFrame. Here's some c++ code which does this, which
mComposerAttributeTable = new QgsComposerAttributeTableV2(
mComposition, false );
mComposition->addMultiFrame( mComposerAttributeTable );
mFrame1 = new QgsComposerFrame( mComposition, mComposerAttributeTable,
5, 5, 100, 30 );
mFrame2 = new QgsComposerFrame( mComposition, mComposerAttributeTable,
5, 40, 100, 30 );
mComposerAttributeTable->addFrame( mFrame1 );
mComposerAttributeTable->addFrame( mFrame2 );
mComposition->addComposerTableFrame( mComposerAttributeTable, mFrame1 );
mComposition->addComposerTableFrame( mComposerAttributeTable, mFrame2 );
mComposerAttributeTable->setVectorLayer( mVectorLayer );
mComposerAttributeTable->setDisplayOnlyVisibleFeatures( false );
mComposerAttributeTable->setMaximumNumberOfFeatures( 10 );
mComposerAttributeTable->setContentFont(
QgsFontUtils::getStandardTestFont() );
mComposerAttributeTable->setHeaderFont(
QgsFontUtils::getStandardTestFont() );
mComposerAttributeTable->setBackgroundColor( Qt::yellow );
When in doubt for something like this and you can't find any examples
https://github.com/qgis/QGIS/tree/master/tests/src
(or in this case
https://github.com/qgis/QGIS/blob/master/tests/src/core/testqgscomposertablev2.cpp
)
They are filled with minimal test cases such as this which are useful
for seeing how various classes are intended to be used.
Nyall
Post by James Stott
I cant find any documentation or examples on how to do this. So if
someone
Post by James Stott
could shed some light on this it would be most appreciated. I am happy to
write up a bit of documentation once I have managed to do this.
Many thanks,
James
_______________________________________________
Qgis-developer mailing list
http://lists.osgeo.org/mailman/listinfo/qgis-developer
Loading...