你能告诉我如何用自定义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 );
}
下面是一个来自QWidget的paintEvent示例。您应该能够将其调整为您的paint事件(您将使用您的bound变量而不是"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();
}