0

您能否告诉我如何通过自定义 QGraphicsItem绘制具有这样边界的矩形。我做了布局并绘制了矩形,但我的矩形的边界是普通的笔。请求您的帮助。谢谢。

void QGraphicsTileItem::paint(QPainter *painter,
                              const QStyleOptionGraphicsItem *option,
                              QWidget *widget)
{
    /// Draw the rect
    painter->setRenderHints( QPainter::Antialiasing |
                             QPainter::TextAntialiasing |
                             QPainter::SmoothPixmapTransform |
                             QPainter::HighQualityAntialiasing, true );
    painter->setPen( pen );
    painter->setBrush( colour );
    painter->drawRect( bound );
    painter->drawText( bound, Qt::AlignLeft | Qt::AlignTop |
                       Qt::TextSingleLine, name );
}
4

1 回答 1

2

这是来自 QWidget 的paintEvent 的示例。您应该能够使其适应您的绘画事件(您将使用绑定变量而不是“this”):

void Button::paintEvent(QPaintEvent *e)
{
    QPainter paint;
    paint.begin(this);
    paint.setPen(Qt::white);
    paint.drawLine(0,0, this->width(), 0);
    paint.drawLine(0,0, 0, this->height());
    paint.setPen(Qt::black);
    paint.drawLine(0, this->height()-1, this->width()-1, this->height()-1);
    paint.drawLine(this->width()-1, 0, this->width()-1, this->height()-1);
    paint.end();
}
于 2013-03-17T08:10:22.073 回答