Здесь довольно странная ситуация. У меня есть класс, который производный от QGraphicsSceneQT 4.5 - QGraphicsItem присваивается родительской сцене 0x0
class DrawingScene : public QGraphicsScene
{
...
}
Всякий раз, когда я добавить любой элемент в производном классе, элемент кончались присоединения к родителю 0x0. Дело в том, что, несмотря на это, графический элемент, появляющийся внутри сцены, как обычно, и с которым можно взаимодействовать. Например, этот фрагмент кода:
QGraphicsEllipseItem * newEllipse = addEllipse(rect, pen);
qDebug() << "Scene info " << this
newEllipse->setPos(pos);
newEllipse->setZValue(100);
qDebug() << "New point added = " << newEllipse;
Вот некоторые depug OUTPUT
Scene info DrawingScene(0x1d2ffb8)
New point added = QGraphicsItem(this = 0x3f0fda0 , parent = 0x0 , pos = QPointF(326, 307), z =100, flags = {"isVisible|isEnabled" })
И всякий раз, когда я пытаюсь удалить элемент, я получаю предупреждение о том, что сцена отличается, но деталь тем не менее.
Я что-то упускаю, или QGraphicsScene не должен быть получен изначально?
Edit: Код для удаления
В QGraphicsItems, добавляют в QMap для быстрого отображения цели и отключить их группу на ходу. Так что это как элемент добавляется к сцене (как указано выше) и Карта
int SceneTrackerData::addLink(QGraphicsLineItem * newLink)
{
links_.insert(currentLinkID_, newLink);
qDebug() << links_;
// Store the ID of the newly added link and increment link count
int thisLinkID = currentLinkID_;
currentLinkID_++;
newLink->setData(LinkData::LinkID, QVariant(thisLinkID));
qDebug() << "Link " << thisLinkID << " connecting " << newLink->data(0).toInt()
<< " and " << newLink->data(1).toInt() << " added.";
return thisLinkID;
}
Вот исключение:
SceneManager содержит экземпляр DrawingScene, который наследует QGraphicsScene
void SceneManager::DeleteLink(int linkID, int startNodeID, int endNodeID)
{
// sceneData_ wraps the QMap<int, QGraphicsItem>.
// This calls SceneTrackerData::deleteLink(int) (below)
QGraphicsLineItem * link = sceneData_->deleteLink(linkID);
qDebug() << "Link to remove " << link;
// scene_ is an instance of DrawingScene, which inherits QGraphicsItem, so
// this is just a vanila call to QGraphicsScene::removeItem()
scene_->removeItem(link);
// release the link from memory
delete link;
emit informLinkDeleted(startNodeID, endNodeID);
}
QGraphicsLineItem* SceneTrackerData::deleteLink(int linkid)
{
qDebug() << "deleting link of id " << linkid;
QGraphicsLineItem * line = links_.take(linkid);
qDebug() << links_;
return line;
}
Предупреждение:
QGraphicsScene::removeItem: item 0x32fa7d8's scene (0x0) is different from this scene (0x1aabb60)
Дополнительные изменения
После добавления некоторых линий и элементов эллипса, это выглядит так: scene-> items() после добавления объектов. Все они имеют родительскую = 0x0
(QGraphicsItem(this =0x3b980a8, parent =0x0, pos =QPointF(0, 0),
z =-1, flags = {"isVisible|isEnabled" }),
QGraphicsItem(this =0x3c32980, parent =0x0, pos =QPointF(326, 303),
z =100, flags = {"isVisible|isEnabled" }),
QGraphicsItem(this =0x3b9a1e0, parent =0x0, pos =QPointF(420, 410),
z =100, flags = {"isVisible|isEnabled" }),
QGraphicsItem(this =0x3f0ffc0, parent =0x0, pos =QPointF(411, 395),
z =100, flags = {"isVisible|isEnabled" }),
QGraphicsItem(this =0x3cf4600, parent =0x0, pos =QPointF(327, 302),
z =0, flags = {"isVisible|isEnabled" }))
То, что я пытаюсь удалить это QGraphicsItem (это = 0x3cf4600), и вот результат:
Link to remove QGraphicsItem(this = 0x3cf4600 , parent = 0x0 , pos = QPointF(327,
302), z =0, flags = {"isVisible|isEnabled" })
Before removing:
QGraphicsScene::removeItem: item 0x3cf4600's scene (0x0) is different from this scene
(0x1e2ffb8)
Scene after moving
(QGraphicsItem(this =0x3b980a8, parent =0x0, pos =QPointF(0, 0),
z =-1, flags = {"isVisible|isEnabled" }),
QGraphicsItem(this =0x3c32980, parent =0x0, pos =QPointF(326, 303),
z =100, flags = {"isVisible|isEnabled" }),
QGraphicsItem(this =0x3b9a1e0, parent =0x0, pos =QPointF(420, 410),
z =100, flags = {"isVisible|isEnabled" }),
QGraphicsItem(this =0x3f0ffc0, parent =0x0, pos =QPointF(411, 395),
z =100, flags = {"isVisible|isEnabled" }))
поведение является правильным. Я озадачен, почему предупреждение навеяно вечером.
В чем причина получения QGraphicsScene? – erelender
Чтобы перехватить события mouseclickat, mousepressed и т. Д. – Extrakun
@Extrakun вы можете поделиться своим кодом для событий мыши. Я также борюсь с подобными проблемами. –