如何解决为什么未将我的孩子名单与该项目一起添加到场景中?
因此,Scene-> addItem()将一个项及其所有子项添加到当前场景,我想我理解这是因为它对于其他类正常工作,当我将该项添加到场景时,他的所有属性都被添加了。这个项目有10个项目,我将它们放入列表中,但是没有添加到场景中,我必须在Game类中手动添加它,有人可以指出我做错了什么吗? 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,1200,700);
setScene(scene);
//set cursor;
cursor = new QGraphicsRectItem();
cursor->setRect(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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。