繁体   English   中英

Qt QGraphicsItem在单独类的boundingRect()中绘制多边形吗?

[英]Qt QGraphicsItem drawing a polygon within the boundingRect() of separate class?

我已经在新类中创建了GraphicsItem并绘制了多边形,但是,不是在类的boundingRect()内绘制它,而是在主GraphicsView上的坐标处绘制,希望它会在boundingRect()中绘制)。

Detector::Detector()
{
    Pressed = false; //Initally the pressed boolean is false, it is not pressed
    setFlag(ItemIsMovable);
}    

QRectF Detector::boundingRect() const
{
    return QRectF(780,425,70,40);
}

void Detector::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QRectF ItemBoundary = boundingRect();
    QBrush *fillBrush = new QBrush(QColor(83,71,65));


    QPolygon DetectorPolygon;
    DetectorPolygon << QPoint(0,0);
    DetectorPolygon << QPoint(20,10);
    DetectorPolygon << QPoint(70,10);
    DetectorPolygon << QPoint(70,20);
    DetectorPolygon << QPoint(20,20);
    DetectorPolygon << QPoint(0,40);

    QPen borderPen;
    borderPen.setWidth(2);
    borderPen.setColor(QColor(152,133,117));

    painter->setBrush(*fillBrush);
    painter->setPen(borderPen);
    painter->drawPolygon(DetectorPolygon);


//    painter->fillRect(ItemBoundary,*fillBrush);
//    painter->drawRect(ItemBoundary);

}

最后两行在未注释掉时将用矩形填充boundingRect(),与上面的多边形不同,我可以将ItemBoundary变量传递给它。

如何将ItemBoundary(= BoundingRect())传递给多边形?

编辑:本质上,我想绘制一个多边形,该多边形可以移动并作为一个单独的类,以发送到我的主用户界面中的QGraphicsView。

正如@FrankOsterfeld所说:

painter->translate(780,425);

将项目移到boundingRect()所在的区域中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM