0

I'm trying to group a list of QGraphicsPixmapItems in a QGraphicsScene. However, I see that I can only add lists of QGraphicsItems to the scene. What would be the best way to typecast a list of QGraphicsPixmapItems to a list of QGraphicsItems? Is there a specific way to do this for qt items? Alternatively, is there a better way to add a list of QGraphicsPixmapItems to a scene group?

Just to illustrate the problem, in the code below, I've created 5 blank pixmaps that I'm trying to add.

QGraphicsScene *scene;
QColor whiteColor = QColor(0,0,0,0);
QGraphicsItemGroup * labels;
QList<QGraphicsPixmapItem*> labelItems;
for (int i = 0; i<5; i++)
{       
    QGraphicsPixmapItem *labtemp = new QGraphicsPixmapItem();       
    QPixmap labelMap = QPixmap(100,100);
    labelMap.fill(whiteColor);
    labtemp->setPixmap(labelMap);
    labelItems.append(labtemp);
    scene->addItem(labelItems[i]);
}

labels = scene->createItemGroup(labelItems);

Any help would be appreciated. Thanks!

  • *Is there a specific way to do this for qt items?*: No, Qt do not implement any function for what you point out. Qt is written in C++, so everything that works in C++ works in Qt. – eyllanesc Mar 03 '19 at 04:11
  • Thanks! To clarify, if I want to add a QGrpahicsPixmapItems to a group, the best way would be essentially to convert each of the items to a QGraphicsItem. Is that correct? – CSforStructuralEngineer Mar 03 '19 at 04:14
  • There is no better way because the methods are indifferent in a matter of time and memory, a simple solution is to create the group_item and add it one by one: `QGraphicsItemGroup * labels = new QGraphicsItemGroup;` `for (int i = 0; i<5; i++) { QGraphicsPixmapItem *labtemp = new QGraphicsPixmapItem(); label->addToGroup(labtemp);}` – eyllanesc Mar 03 '19 at 04:17
  • Have you read the answers to the duplicate question? There he explains more possible solutions to you in detail. – eyllanesc Mar 03 '19 at 04:18
  • Yes, although someone commented that it wouldn't be safe for parent and child classes – CSforStructuralEngineer Mar 03 '19 at 04:20
  • Thanks for the suggestion about creating a new group directly. I think I will do that. Is there an easy way to access members in a group like a list in the order in which they were added? Like labels[i]->setPixmap() – CSforStructuralEngineer Mar 03 '19 at 04:21
  • You have to do a casting. – eyllanesc Mar 03 '19 at 04:23

0 Answers0