【问题标题】:qt QGraphicsScene additemqt QGraphicsScene additem
【发布时间】:2014-10-02 18:03:36
【问题描述】:

http://qt-project.org/doc/qt-4.8/qgraphicsscene.html#addItem

如果该项目已经在不同的场景中,它将首先从它的旧场景中移除,然后作为一个添加到这个场景中 顶级。

我想将项目保留在旧场景中。 我该怎么做?

myscene1.addItem(item);
myscene2.addItem(item);// I don't want to remove item from myscene1

【问题讨论】:

    标签: c++ qt qgraphicsscene


    【解决方案1】:

    您可以复制该项目:

    myscene1.addItem(item);
    myscene2.addItem(item->clone());
    

    【讨论】:

    • 通过实现克隆功能解决。
    【解决方案2】:

    一个项目不能同时占据两个场景,就像你不能同时在两个地方一样。

    这样做的唯一方法是复制该项目并将其放置在第二个场景中。

    【讨论】:

      【解决方案3】:

      你可以做的是创建一个新类。例如

      class Position
      {
         ...
         QPoinfF pos;
         ...
      }
      

      然后您可以将该类添加到您的项目中。

      class Item : public QGraphicsItem
      {
         ...
      public:
         void setSharedPos(Position *pos)
         {
            sharedPosition = pos;
         }
      
         //implement the paint(...) function
         //its beeing called by the scene
         void paint(...)
         {
            //set the shared position here
            setPos(sharedPos);
            //paint the item
            ...
         }
      protected:
         void QGraphicsItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
         {
            //get the position from the item that could have been moved
            //you could also check if the position actually changed
            sharedPosition->pos = pos();
         }
      
      private
         Position *sharedPostion;
         ...
      }
      

      您不必创建两个项目并为它们提供指向位置对象的相同指针。

      Item *item1 = new Item;
      Item *item2 = new Item;
      Position *sharedPos = new Position;
      
      item1->setSharedPos(sharedPos);
      item2->setSharedPos(sharedPos);
      
      myScene1->addItem(item1);
      myScene2->addItem(item2);
      

      他们至少不应该分享他们在场景中的位置。 如果这可行,那么您必须更改 Position 类以满足您的需求,并且它应该可以正常工作。

      我不太确定在paint() 函数中设置位置是否有效。但这就是我尝试同步项目的方式。如果它不起作用,那么您将不得不寻找另一个地方来更新项目的设置。

      或者你可以给项目一个指针,让它们直接改变位置/设置。

      例如

      class Item : public QGraphicsItem
      {
      ...
         void QGraphicsItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
         {
             otherItem->setPos(pos());
         }
      ...
         void setOtherItem(Item *item)
         {
            otherItem = item;
         }
      private:
         Item *otherItem;
      }
      
      Item *item1 = new Item;
      Item *item2 = new Item;
      
      item1->setOtherItem(item2);
      item2->setOtherItem(item1);
      
      myScene1->addItem(item1);
      myScene2->addItem(item2);
      

      【讨论】:

      • OP 没有提到同步两个对象的要求。但是,在这种情况下,更好的方法是链接两个对象,并在 moveEvent 上,向链接对象发送信号以进行更新,但要注意不要以相互发送信号的循环结束,方法是检查是否新位置等于旧位置。
      • 你是对的,他没有提到任何关于同步的事情。我只是试图向他解释如何让他们以他希望的方式表现得一样。这只是我为了实现我认为他想要实现的行为而想到的概念。同步项目的位置只是我的一个例子。他也没有提到这一点。
      猜你喜欢
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多