Gamma
Gamma

Reputation: 347

QGraphicsItem Border Styles

How can I draw a rectangle shape and oval shape like this image.In this code which creates rectangle shape and oval shape with a single line border.But I need to change there border style like this given image.

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);

    QBrush redBrush(Qt::red);
    QBrush blueBrush(Qt::blue);
    QPen blackPen(Qt::black);
    blackPen.setWidth(6);

    //rect = scene->addRect(25,25,Qt::RelativeSize);
    elipse = scene->addEllipse(20,20,100,100,blackPen,redBrush);
    elipse->setFlag(QGraphicsItem::ItemIsMovable, true);
    rect = scene->addRect(-20,-10,200,100,blackPen,blueBrush);
    rect->setFlag(QGraphicsItem::ItemIsMovable, true);
    rect->setFlag(QGraphicsItem::ItemIsSelectable,true);
}

Expected Result image

Upvotes: 1

Views: 3293

Answers (1)

eyllanesc
eyllanesc

Reputation: 244301

These properties do not have the QGraphicsItem that Qt provides, what we must do is create our custom QGraphicsItem. An option is to inherit from classes that are standard:

dgraphicsitem.h

#ifndef DGRAPHICSITEM_H
#define DGRAPHICSITEM_H

#include <QGraphicsRectItem>
#include <QPainter>

class DGraphicsEllipseItem : public QGraphicsEllipseItem
{
public:
    DGraphicsEllipseItem(const QRectF &rect, qreal radius, QGraphicsItem *parent = nullptr):
        QGraphicsEllipseItem(rect, parent){
        mRadius = radius;
    }
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
        QGraphicsEllipseItem::paint(painter, option, widget);
        painter->setBrush(brush());
        QRectF r = rect();
        r.setSize(r.size()-mRadius*QSizeF(1, 1));
        r.translate(mRadius*QPointF(1, 1)/2);
        painter->drawEllipse(r);
    }
private:
    qreal mRadius;
};

class DGraphicsRectItem : public QGraphicsRectItem
{
public:
    DGraphicsRectItem(const QRectF &rect, qreal radius, QGraphicsItem *parent = nullptr):
        QGraphicsRectItem(rect, parent){
        mRadius = radius;
    }
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
        QGraphicsRectItem::paint(painter, option, widget);
        painter->setBrush(brush());
        QRectF r = rect();
        r.setSize(r.size()-mRadius*QSizeF(1, 1));
        r.translate(mRadius*QPointF(1, 1)/2);
        painter->drawRect(r);
    }
private:
    qreal mRadius;
};
#endif // DGRAPHICSITEM_H

then it is added to the scene:

scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);

QBrush brush(Qt::white);
QPen pen(Qt::green);
pen.setWidth(1);

DGraphicsEllipseItem* ellipse = new DGraphicsEllipseItem(QRectF(20,20, 200,100), 10);
scene->addItem(ellipse);
ellipse->setPen(pen);
ellipse->setBrush(brush);
ellipse->setFlag(QGraphicsItem::ItemIsMovable, true);

DGraphicsRectItem* rect = new DGraphicsRectItem(QRectF(-20,-10,200,100), 10);
scene->addItem(rect);
rect->setPen(pen);
rect->setBrush(brush);
rect->setFlag(QGraphicsItem::ItemIsMovable, true);

Output:

enter image description here

The complete example can be found in the following link.

Upvotes: 3

Related Questions