C++ 为什么我的孩子列表没有与项目一起添加到场景中?

C++ 为什么我的孩子列表没有与项目一起添加到场景中?,c++,qt,parent-child,child-process,appendchild,C++,Qt,Parent Child,Child Process,Appendchild,因此,Scene->addItem()将一个项及其所有子项添加到当前场景中,我想我理解它,因为它在其他类中正常工作,当我将该项添加到场景中时,他的所有属性都被添加。这一个有10个项目,我放在一个列表中,但没有被添加到场景中,我必须在游戏类中手动添加它,有人能指出我做错了什么吗?Qlist的实现是否正确?我只想做我通常在容器中声明指针的方式,因为Qt有这个列表,我想用它来代替一个简单的数组 Game.cpp #include "game.h" #include <QDeb

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

Game.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) {

   }
}

#包括“game.h”
#包括
#包括
#包括“process.h”
游戏::游戏():QGraphicsView()
{
//布景;
场景=新的Qgraphicscene(此);
场景->设置场景(0,01200700);
场景(场景);
//设置光标;
游标=新的QGraphicsRectItem();
游标->setRect(0,0100);
光标->退刀(Qt::蓝色);
场景->添加项(光标);
光标->隐藏();
setMouseTracking(真);
//改变窗口
设置固定大小(1200700);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
拖动=假;
//创建队列
q=新的ProcessQueue(此);
场景->附加项(q);
//当我运行代码时,它不会自动添加进程,我必须手动添加
对于(inti=0;iqueue.size();i++)
{
场景->添加项(q->处理(i));
}
//暂停按钮
p=新的暂停按钮(此按钮);
场景->附加项(p);
//制造核心
p1=新的CpuCore(本);
p1->setPos(300250);
p1->设置值(1);
场景->附加项(p1);
场景->附加项(p1->slot1);
p1->slot1->setPos(3055255);
场景->附加项(p1->slot2);
p1->slot2->setPos(420255);
场景->附加项(p1->slot3);
p1->slot3->setPos(305370);
场景->添加项(p1->插槽4);
p1->slot4->setPos(420370);
}
void Game::mouseMoveEvent(QMouseEvent*事件){
//使光标移动
如果(光标)
光标->设置位置(事件->位置());
}
bool Game::SlotIsEmpty(QMouseEvent*event,ProcessSlot*slot)
{
如果(插槽->包含(插槽->mapFromScene(事件->位置()))
{
对于(int i=0;iSize();i++)
{
如果(q->processAt(i)->包含(q->processAt(i)->mapFromScene(event->pos())&&q->processAt(i)!=拖动进程)
返回false;
}
返回true;
}
返回false;
}
void Game::mousePressEvent(QMouseEvent*event){
试一试{
//如果我们还没有拖动进程,那么单击的进程将是被拖动的进程
如果(!拖动)
{
对于(int i=0;iSize();i++)
{
如果(q->processAt(i)->包含(q->processAt(i)->mapFromScene(event->pos()))
{
拖动_process=q->processAt(i);

()请提供一个。此代码不显示
场景
游戏
类和方法。这也不意味着在此处复制粘贴更多代码,示例应该是最小的-即剥离所有不必要的代码。您是否尝试调试应用程序并查看发生了什么?我添加了更多代码,场景在游戏类中,我希望现在更容易理解了。抱歉,这比我想通读的代码要多,我想看看是否值得花时间调试。您可能需要自己进行更多的调试,以缩小问题的范围。(例如,您能想出一些方法来检查您的
QList
,独立于场景吗?)如果你觉得帮助初学者不值得花时间,那么就不要费心了。@user29893我并没有说帮助初学者不值得我花时间。我说调试可能不值得我花时间。如果代码风格合理,没有bug,调试就值得我花时间(对于初学者来说,这个标准不太高;事实上,我在代码中看到了有意义的变量名和一致的缩进,这是很好的迹象,表明您可能已经达到了这个标准)。但是,我不会阅读那么多代码来查看它处于什么状态。如果您不想花时间创建一个,那么我就不想花时间阅读示例。
#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

#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();
}
#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