QGraphicsView周围的自定义边框

时间:2016-03-11 12:52:42

标签: c++ qt border qgraphicsview

我有这个图片: enter image description here

我希望GraphicsView的边框与此图像完全相同。

(考虑到波纹管图像我希望顶部的白色矩形变成那个形状)enter image description here

我从QGraphicsView继承了一个类,并尝试在drawBackgroundpaintEvent中绘制此图像,但它们都不起作用。

我的代码:

.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

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

1)创建一个具有图像大小的QGraphicsScene

2)将其设置为视图的场景

3)在你正在做的drawBackground中画画

答案 1 :(得分:0)

我尝试了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"));
}

结果:

enter image description here