[Qgis-developer] Add multiple frames to a composer attribute table using PyQGIS

James Stott jamesstott1 at gmail.com
Tue Nov 3 01:31:02 PST 2015


Hi Nyall,

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

That was an excellent tip about checking the unit tests.

James

On Sun, 1 Nov 2015 at 10:23 Nyall Dawson <nyall.dawson at gmail.com> wrote:

> On 1 November 2015 at 20:09, James Stott <jamesstott1 at gmail.com> wrote:
> > 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 (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
>
>
>
>
> >
> > 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
> > Qgis-developer at lists.osgeo.org
> > http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20151103/bcfc89de/attachment.html>


More information about the Qgis-developer mailing list