0

カスタムQGraphicsItemでQPainterによって描かれた楕円にQGraphicsPolygonItemを配置しようとしています。私の問題は次のとおりです。楕円を灰色のグラデーションカラーで塗りつぶし、長方形を赤色で塗りつぶしました。問題は全体的な表示にあります。QGraphicsPolygonItem は、boundingrect の白い背景を示しています。私の質問は、どうすれば削除できますか?!

白い背景を削除したい

編集:私のペイント機能

QPoint p1(0,0);
QPoint p2(10, 8);

painter->setPen(Qt::NoPen);
painter->setBrush(Qt::lightGray);
painter->drawEllipse(p2, 100, 100);

painter->setBrush(Qt::gray);
painter->setPen(Qt::black);
painter->drawEllipse(p1, 100, 100);

myPolygon = new QGraphicsPolygonItem(myPolygonPoints, this);
myPolygon->setBrush(Qt::red);
myPolygon->setPen(Qt::NoPen);
myPolygon->show();

これは私のカスタム QGraphicsItem のペイント機能です。

4

1 に答える 1

0

まず、ペイント関数で QGraphicsPolygonItem を作成するのは非常に悪いです。ペイント関数が呼び出されるたびに新しいアイテムが作成され、グラフィックス シーンに追加され、最終的にはメモリ不足になります!

アイテムを親子化すると、すぐにシーンに追加されるので、myPolygon->show(); を呼び出すべきではありません。

派生した MyGraphicsItem クラスで QGraphicsPolygonItem をインスタンス化します...

class MyGraphicsItem : public QGraphicsItem
{
    public:
        MyGraphicsItem(QGraphicsItem* parent);

    private:
        QGraphicsPolygonItem* m_pMyPolygon;
}


MyGraphicsItem::MyGraphicsItem(QGraphicsItem* parent)
    : QGraphicsItem(parent)
{

    // assume you've created the points...

    m_pMyPolygon = new MyPolygonItem(myPolygonPoints, this);

    // set the brush and pen for the polygon item
    m_pMyPolygon->setBrush(Qt::transparent);
    m_pMyPolygon->setPen(Qt::red);
}

ポリゴン アイテムは graphicsItem の親であるため、自動的に表示されます。

于 2013-11-07T10:00:14.913 に答える