我在一个新类中创建了一个 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。