1

すべて、ポリゴンであるQGraphicsItemを実装しました。QGraphicsEllipseItem(ドラッグ機能の)ポイントとして開発をスピードアップしましたが、update()機能的に困っています。私のコードは最後に投稿されています、私の質問は次のとおりです:

  1. 私はここで正しいアプローチを取っていますか?私は自分自身を疑うようになり始めています
  2. QGraphicsItem::update()私は自分の実装を呼び出すことになっていますか?私はそれをたくさん呼んでいます

その他の情報:

  • 私は汚い小さなハックをしました。私の実際のコードでは、からも継承していQObjectます。eventFilterこれにより、シーン()にをインストールできます(これは、を使用して設定されていることがわかっていますitemChange)。QGraphicsSceneMouseEventマウスの移動中にフィルタリングして呼び出すシーンから、QGraphicsItem::update()そうしないと線が再描画されません。
  • 今シーンから自分を削除してInteractivePolygonも、線は削除されません!私はscene()->updateを呼び出さなければなりません。何かがおかしい気がします。

宣言:

class InteractivePolygon : public QGraphicsItem
{

public:
   //Only important methods
   QRectF boundingRect() const;
   void paint(bla bla bla);
   bool eventFilter(QObject *, QEvent *);

private:
   QList<QGraphicsEllipseItem *> m_points;

   void AddPolygonPoint(QPointF);
   QGraphicsEllipseItem * MakeNewPoint(QPointF);
}

実装:

QRectF InteractivePolygon::boundingRect() const
{
   return childrenBoundingRect();
}

void InteractivePolygon::paint(QPainter painter.. otherstuf)
{
   QPen line_pen(QColor(255,0,0));
   painter->setPen(line_pen);

   if(m_points.count() > 1)
   {
      for(int i = 1; i < m_points.count(); ++i)
          painter->drawLine(m_points[i-1]->pos(), m_points[i]->pos());
   }
}

void AddPolygonPoint(QRectF point)
{
   QGraphicsEllipseItem * new_item = MakeNewPoint(point);
   new_item->setParent(this);

   m_points->push_front(new_item);

   update();  
}

QGraphicsEllipseItem * InteractivePolygon::MakeNewPoint(QPointF & new_point)
{
   QGraphicsEllipseItem * result = 0;
   result = new QGraphicsEllipseItem();
   result->setPos(new_point);
   result->setRect(-4, -4, 8, 8);
   result->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable)

   return result;
}

//Lets pretend this method is correctly setup/exists
bool InteractivePolygon::eventFilter(QObject *object, QEvent *event)
{
   if(event->type() == QEvent::QEvent::GraphicsSceneMouseMove)
   {
      QGraphicsSceneMouseEvent * mouse_move = (QGraphicsSceneMouseEvent *)event;
      //Selected index is set else, let's assume it works
      if(selected_index)
      {
         update(); //If I don't do this, my lines in my paint() are not redrawn.
      }
   }
}
4

1 に答える 1

0

解決策はprepareGeometryChange()、バウンディングボックスを変更するアイテムの何かを変更するたびにでした。すべてが正しく再描画され、必要に応じて更新されます。

update()これにより、すべての通話を削除できました。

于 2012-11-08T14:42:19.983 に答える