Search code examples
c++qtborderqgraphicsview

Custom border around QGraphicsView


I have this image : enter image description here

I want the border of my GraphicsViewbecome exactly like this image.

(consider the bellow image I want the top white rectangle become that shape)enter image description here

I inherit a class from QGraphicsView and tried drawing this image in both drawBackground and paintEvent but none of them works.

My code :

.h file

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

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

}; 

.cpp file

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

result of my code : https://i.sstatic.net/r0waP.png

Any suggestion ?


Solution

  • I tried setMask() and now it works fine .

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

    result :

    enter image description here