PDA

View Full Version : Arrays of QPixmaps and QGraphicsPixmapItems



mdavidjohnson
28th October 2016, 05:10
I would like to build up a QGraphicsScene using a 10x10 arrangement of QGraphicsPixmapItems (a chessboard with a border). Something like this:

Pseudocode:


QPixmap *pixmap( 10, 10 );
pixmap( 0, 0 ) = new QPixmap( "Image00.png" );
pixmap( 0, 1 ) = new QPixmap( "Image01.png" );
pixmap( 0, 2 ) = new QPixmap( "Image02.png" );
.
.
.
pixmap( 9, 9 ) = new QPixmap( "Image99.png" );

QGraphicsPixmapItem pixmapItem( 10, 10 );
pixmapItem( 0, 0 ).setPixmap( *pixmap( 0, 0 ) );
pixmapItem( 0, 0 ).setPos( -370, -370 );
pixmapItem( 0, 1 ).setPixmap( *pixmap( 0, 1 ) );
pixmapItem( 0, 1 ).setPos( -296, -370 );
pixmapItem( 0, 2 ).setPixmap( *pixmap( 0, 2 ) );
pixmapItem( 0, 2 ).setPos( -222, -370 );
.
.
.
pixmapItem( 9, 9 ).setPixmap( *pixmap( 9, 9 ) );
pixmapItem( 9, 9 ).setPos( 296, 296 );

But there doesn't seem to be any way to make arrays of QPixmaps or arrays of QGraphicsPixmapItems; at least not anywhere that I've seen in the docs.

Am I missing something?

Or could anyone suggest a workable line of attack?

jefftee
28th October 2016, 05:30
Have you tried something like the following (untested)?



QVector< QVector< QPixmap > > pixmap_array;
pixmap_array.resize(10);

for (int i = 0; i < pixmap_array.size(); i++)
{
pixmap_array[i].resize(10);
}

pixmap_array[0][0] = QPixmap(...);
pixmap_array[0][1] = QPixmap(...);
...
pixmap_array[9][9] = QPixmap(...);

anda_skoa
28th October 2016, 11:02
And don't allocate QPixmap on the heap, that just means you will have to manually delete them again.

Cheers,
_

mdavidjohnson
30th October 2016, 12:14
Yes,

That looks like it should work.

I'll give it a try.

Thanks!