【问题标题】:How to remove BoundingRect from displaying in QGraphicPolygonItem如何从 QGraphicPolygonItem 中删除 BoundingRect
【发布时间】:2013-11-18 20:50:42
【问题描述】:

我正在尝试将 QGraphicsPolygonItem 放在由 QPainter 在自定义 QGraphicsItem 中绘制的椭圆上。我的问题如下。我用灰色渐变填充了椭圆,并用红色填充了我的矩形。现在问题出在整体显示上。 QGraphicsPolygonItem 显示边界矩形的白色背景。我的问题是如何删除它?!

编辑:我的绘画功能

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 的绘图功能。

【问题讨论】:

  • 你能展示你为自定义 QGraphicsItem 绘制的函数吗?

标签: qt user-interface graphics qgraphicsitem


【解决方案1】:

首先,在绘制函数中创建 QGraphicsPolygonItem 真的很糟糕;每次调用paint函数时都会创建一个新项目并将其添加到图形场景中,最终您将耗尽内存!

当您将项目作为父项时,您会立即将其添加到场景中,因此您不应该调用 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的父级,它会自动显示出来。

【讨论】:

  • 这不起作用。请参阅编辑。我写了painter->setBrush(Qt::transparent);在painter->drawEllipse(p1, 100, 100);之后
  • 感谢您的代码。我对下一节中的代码有一个疑问。画家->setBrush(Qt::transparent);画家->setPen(Qt::red);我使用现有代码中的代码只是为了检查画笔和笔的行为。它没有消除白色背景。它只是删除了红色的填充。现在它在白色边界矩形中显示了一条带有白色背景的红色多边形线。
  • 我已经简化了答案,让我知道这是怎么回事。
  • 还是一样。它在白色边界矩形中显示带有白色背景的红色多边形线
猜你喜欢
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 2011-01-12
  • 1970-01-01
  • 2018-07-29
  • 2011-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多