QGraphicsView 周围的自定义边框

Custom border around QGraphicsView

我有这张图片:

我希望 GraphicsView 的边框与这张图片完全一样。

(考虑下面的图像,我希望顶部的白色矩形变成那个形状)

我从 QGraphicsView 继承了一个 class 并尝试在 drawBackgroundpaintEvent 中绘制此图像,但其中 none 有效。

我的代码:

.h 文件

class GraphicsTraxSuggestionView : public QGraphicsView {
    Q_OBJECT
public:
    GraphicsTraxSuggestionView(QWidget* widget);

protected:
//  void paintEvent(QPaintEvent *event);
    void drawBackground(QPainter *p, const QRectF &rect);
private:

}; 

.cpp 文件

GraphicsTraxSuggestionView::GraphicsTraxSuggestionView(QWidget* widget)
    : QGraphicsView(widget)
{
    //setFrameShadow(QFrame::Raised);
    setFrameStyle(QFrame::NoFrame);
    setStyleSheet("QGraphicsView { border-style: none; }");
}
void GraphicsTraxSuggestionView::drawBackground(QPainter *painter, const QRectF &rect)
{
    painter->drawImage(rect, QImage("suggestionBorder.png"));
}

我的代码的结果:http://i.stack.imgur.com/r0waP.png

有什么建议吗?

1) 创建一个 QGraphicsScene 大小为你的图像

2) 设置为视图的场景

3) 像现在一样画 drawBackground

我尝试了 setMask(),现在可以正常使用了。

GraphicsTraxSuggestionView::GraphicsTraxSuggestionView(QWidget* widget,QGraphicsScene* scene)
    : QGraphicsView(widget),
    scene_(scene)
{
    setStyleSheet("background-color: transparent;");

    QPixmap myPixmap = QPixmap(":/Game/Tiles//suggestionBorder.png").scaled
        (scene_->sceneRect().size().width(),scene_->sceneRect().size().height());
    setMask(myPixmap.mask());
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}

void GraphicsTraxSuggestionView::drawBackground(QPainter *painter, const QRectF &rect)
{   
    painter->drawImage(scene_->sceneRect(), QImage(":/Game/Tiles//suggestionBorder.png"));
}

结果: