首页 > 解决方案 > 为什么我的孩子列表没有与该项目一起添加到场景中?

问题描述

因此, Scene->addItem() 将一个项目及其所有子项添加到当前场景,我想我理解它,因为其他类正常工作,当我将项目添加到场景时,他的所有属性都被添加。这个有 10 个项目,我放在一个列表中,但没有添加到场景中,我必须在 Game 类中手动添加它,有人可以指出我做错了什么吗?Qlist 是否正确实施?我只想做我通常声明一个指针但在一个容器内的方式,因为 Qt 有这个列表,我想用它来代替一个简单的数组。

游戏.cpp

#include "game.h"
#include <QDebug>
#include <QGraphicsScene>
#include "process.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) {

   }
}

游戏.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

进程队列.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();
}

进程队列.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

标签: c++qtparent-childchild-processappendchild

解决方案


推荐阅读