PDA

View Full Version : QGraphicsPixmapItem as an array?



vitaR
13th April 2014, 18:27
Hi, I need to save images in an array, but I tried some things, but still not working, need help.
I'm trying to use and set values to a QGraphicsPixmapItem Array (using C++ arrays) and using QList too.

My code here:


QGraphicsPixmapItem pixmapArray[n*m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
QPixmap *icon = new QPixmap(list.at(biDiArray[i][j])); //list of a QString
QGraphicsPixmapItem image;
image.setPixmap(*icon);
pixmapArray[i*10+j] = image;
}
}


I tried to use it as QList too, but didn't work.



for(int i=0;i<this->n;i++){
for(int j=0;j<this->m;j++){
QPixmap *icon = new QPixmap(list.at(mat[i][j]));
QGraphicsPixmapItem *image = new QGraphicsPixmapItem(*icon);
this->pixmapItemList.insert(10*i+j,*image);
}
}

anda_skoa
13th April 2014, 20:21
If you have an array of items, then you need to call setPixmap on those items, not on local instances.

If you want to create items in the loop, then use an array or list of the item pointer type.

Cheers,
_

P.S.: don't create QPixmap on the heap unless you really need to, currently you are leaking memory.

vitaR
13th April 2014, 20:41
If you have an array of items, then you need to call setPixmap on those items, not on local instances.

If you want to create items in the loop, then use an array or list of the item pointer type.

Cheers,
_

P.S.: don't create QPixmap on the heap unless you really need to, currently you are leaking memory.

Thanks it works, by the way, in your last sentence, any suggestions? I really need to do that, but don't know other way/

I figure it out, sorry, I used:
pixmapArray[i*10+j].setPixmap(QPixmap(list.at(mat[i][j])));

its ok?

anda_skoa
13th April 2014, 22:00
Yes, that's ok.

QPixmap is a "value" it can be copied when needed. So the code you had earlier would have worked without "new":


QPixmap icon = QPixmap(list.at(mat[i][j]));
QGraphicsPixmapItem *image = new QGraphicsPixmapItem(icon);


The constructor of QGraphicsPixmapItem takes the QPixmap as "const QPixmap&", i.e. as a constant reference. This is a common indicator that the thing will either be only used locally in the function (here constructor) or copied.

Cheers,
_