0

I have been using Qt Creator for almost a month now. I hope this is not a silly question because it bugged me for a long time.

I want to achieve:

    1. create many different items(let's say 20 per sec) in QGraphicsScene.
    1. use one single class for all these items
    1. mark all these items, delete or hide them when a signal is triggered.

After trying a lot of methods, the best way I see is to use QGraphicsItemGroup. I tried to group all of them into a QGraphicsItemGroup. But after adding an item into a group, the item refuses to show on the scene any more, no matter what functions I use from library.

Is there something which I did wrongly in using the QGraphicsItemGroup? Or are there any other better apporaches?

Looking forward to anyone's help. Really appreciated.

Followed is a structure of my code:

// ......
QGraphicsItemGroup *myItemsGroup = new QGraphicsItemGroup
timer->start(100);
connect(timer,SIGNAL(timeout()),this,SLOT(draw_trail()));
// ......
void MyFunction::draw_trail()
{
    Trail_item *new_item = new Trail_item;
    scene->addItem(new_item);
    new_item->setX(COPX-7);    
    new_item->setY(COPY-7);
    new_item->setZValue(5);
    /* 
    then I started trying add to group
    //myItemsGroup->addToGroup(new_item)
    after adding this line, the item vanishes.
    */
}

(note: trail_item is a item class I created. Since it is a loop, I don't know how to change *new_item pointer name, so all items have the same name.)

GMchris
  • 5,439
  • 4
  • 22
  • 40
  • Have you added the item group to the scene? The group is visible? – Fabio Aug 01 '16 at 07:04
  • I will check on that right away. Thanks! – Shen Tian Aug 01 '16 at 09:04
  • @Fabio, hi Fabio, sorry for the late reply. I cannot find a function to add a group to scene. I went through the library, I could only found scene->createitemgroup or QGraphicsItemGroup::addToGroup. There are very few functions to use under QGraphicsItemGroup::. However, I know how to set the group visible. – Shen Tian Aug 01 '16 at 11:08
  • QGraphicsItemGroup derive from QGraphicsItem, you can add it to the scene as any other item (QGraphicsScene::addItem) – Fabio Aug 01 '16 at 12:07
  • @Fabio This solved my question! Thanks a lot! This gives me a better understannding of inheritance. – Shen Tian Aug 02 '16 at 01:36
  • 1
    @Fabio you should create an answer and Sehn Tian should accept it. (based on the comments). This will result in a question that has an answer and the problem is solved. – mtb Sep 08 '16 at 13:28

1 Answers1

2

Make sure that you have added the QGraphicsItemGroup in the scene, and that the QGraphicsItemGroup is visible.

You can add the QGraphicsItemGroup to the scene as any other QGraphicsItem, using the method QGraphicsScene::addItem(QGraphicsItem*)

Fabio
  • 2,522
  • 1
  • 15
  • 25