Why my List of children is not being added to the scene with the item?

  appendchild, c++, child-process, parent-child, qt

So, the Scene->addItem() adds an item and all his children to the current scene, i guess i understand it since worked normally for other classes, when i add the item to the scene, all his attributes are added. This one have 10 items that i put inside a list, but is not being added to the scene, i’m having to add it manually in the Game class, can someone point what i’m doing wrong please? Is the Qlist implemented correctly? I just want do how i usually declare a pointer but inside a container, Since Qt have this List i want use it instead a simple Array.

Game.cpp

#include "game.h"
#include <QDebug>
#include <QGraphicsScene>
#include "proccess.h"


Game::Game() : QGraphicsView()
{
   //set scene;
   scene = new QGraphicsScene(this);
   scene->setSceneRect(0,0,1200,700);
   setScene(scene);

   //set cursor;
   cursor = new QGraphicsRectItem();
   cursor->setRect(0,0,100,100);
   cursor->setBrush(Qt::blue);
   scene->addItem(cursor);
   cursor->hide();
   setMouseTracking(true);

   //alter window
   setFixedSize(1200,700);
   setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

   dragging = false;

   //create queue
   q = new ProcessQueue(this);
   scene->addItem(q);
//when i run the code it does not add automatically the processes, i have to add manually
   for(int i=0; i< q->queue.size(); i++)
   {
       scene->addItem(q->processAt(i));
   }

   //pause button
   p = new PauseButton(this);
   scene->addItem(p);

   //making the core
   p1 = new CpuCore(this);
   p1->setPos(300,250);
   p1->setZValue(1);
   scene->addItem(p1);

   scene->addItem(p1->slot1);
   p1->slot1->setPos(305,255);
   scene->addItem(p1->slot2);
   p1->slot2->setPos(420,255);
   scene->addItem(p1->slot3);
   p1->slot3->setPos(305,370);
   scene->addItem(p1->slot4);
   p1->slot4->setPos(420,370);
}


void Game::mouseMoveEvent(QMouseEvent *event){
   //makes the cursor moves
   if (cursor)
       cursor->setPos(event->pos());
}

bool Game::SlotIsEmpty(QMouseEvent* event, ProcessSlot* slot)
{
   if(slot->contains(slot->mapFromScene(event->pos())))
   {
       for(int i=0; i<q->Size(); i++)
       {
           if(q->processAt(i)->contains(q->processAt(i)->mapFromScene(event->pos())) && q->processAt(i) != dragging_process)
               return false;
       }
       return true;
   }
   return false;
}

void Game::mousePressEvent(QMouseEvent *event){

   try {
       //if we are not dragging a process yet, the process clicked will be the dragged process
       if(!dragging)
       {
           for(int i=0; i<q->Size(); i++)
           {
               if(q->processAt(i)->contains(q->processAt(i)->mapFromScene(event->pos())))
               {
                   dragging_process = q->processAt(i);
                   qInfo() << " you are dragging the process " << i;
               }
           }
           QGraphicsView::mousePressEvent(event);
       }


       // if we are already dragging a process, the next click is the point to it be placed.
       else
       {

           //we wanna check if the point clicked is inside an empty slot, if it's not, them we wanna place the process back
           //in the queue

           //check if this slot is occupied
           if(SlotIsEmpty(event,p1->slot1))
           {
               //if is not, change position
               dragging_process->setPos(p1->slot1->x()+5,p1->slot1->y()+5);
           }
           else if(SlotIsEmpty(event,p1->slot2))
           {
               dragging_process->setPos(p1->slot2->x()+5,p1->slot2->y()+5);
           }
           else if(SlotIsEmpty(event,p1->slot3))
           {
               dragging_process->setPos(p1->slot3->x()+5,p1->slot3->y()+5);
           }
           else if(SlotIsEmpty(event,p1->slot4))
           {
               dragging_process->setPos(p1->slot4->x()+5,p1->slot4->y()+5);
           }

           //if is not, position  reamain unchanged.

           dragging_process->show();
           dragging = false;
           dragging_process = nullptr;
           cursor->hide();
       }

   } catch (std::exception const& e) {

   }
}

Game.h

#ifndef GAME_H
#define GAME_H

#include <QObject>
#include <QGraphicsScene>
#include <QGraphicsView>
#include "processqueue.h"
#include "processslot.h"

#include <QGraphicsSceneMouseEvent>
#include <QMouseEvent>
#include <cpucore.h>
#include "pausebutton.h"

class Game : public QGraphicsView
{
public:
    Game();
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    bool dragging;//true if player clicks and drag some process
    bool SlotIsEmpty(QMouseEvent* event, ProcessSlot* slot);//check if process can be placed in a slot

    //pointer to dragging process
    Process* dragging_process;

    //member attributes

    QGraphicsScene* scene;
    QGraphicsRectItem* cursor;
    ProcessQueue* q;
    PauseButton* p;
    CpuCore* p1;

    //




signals:

};

#endif // GAME_H

processQueue.cpp

#include "processqueue.h"
#include "QDebug"
#include "game.h"

extern Game* game;

ProcessQueue::ProcessQueue(QObject* parent)
{
    Q_UNUSED(parent)
    //start up queue
    int d=0;
    for(int i=0; i<10; i++,d+=105)
    {
        Process* p = new Process(this, i+1);
        p->setPos(QPointF(100+d,140));
        p->setZValue(2);
        queue.append(p);
    }

}

void ProcessQueue::enqueue(Process* p)
{
   queue.append(p);
}

Process *ProcessQueue::dequeue()
{
    if(queue.isEmpty())
    {
        qWarning() << "the Queue is Empty";
        return NULL;
    }
     return queue[0];
     queue.pop_front();
}

Process *ProcessQueue::processAt(int i)
{
    if(queue.isEmpty()==false)
        return queue[i];
}

int ProcessQueue::Size()
{
    return queue.size();
}

Processqueue.h

#ifndef PROCESSQUEUE_H
#define PROCESSQUEUE_H

#include <QGraphicsRectItem>
#include <QPen>
#include <QBrush>
#include "process.h"

//The "Queue" is a List implememntd with FIFO policy
class ProcessQueue: public QObject, public QGraphicsRectItem
{
public:
   explicit ProcessQueue(QObject* parent=0);

    void enqueue(Process* p);
    Process *dequeue();
    Process *processAt(int i);
    int Size();


    QList<Process*> queue;
};

#endif // PROCESSQUEUE_H

Source: Windows Questions C++

LEAVE A COMMENT