Qt自定义矩形边界以获得温和的3D效果

时间:2013-03-17 07:45:29

标签: qt qt4 qt5

请告诉我如何通过自定义QGraphicsItem绘制像this这样的边界的矩形。我做了布局并绘制了矩形,但我的rectangles的边界是普通笔。请求你的帮助。感谢。

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 );
}

1 个答案:

答案 0 :(得分:2)

以下是QWidget中的paintEvent示例。你应该能够适应你的绘画事件(你会使用你的绑定变量而不是“这个”):

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();
}