PDA

View Full Version : Problem with adding QGraphicsItem to QChartView



Mohsin
23rd May 2021, 15:30
I have created a line graph using QChartView13641

In this graph, I want to display two squares on top of it, right beside the "Current Hit Ratio" and "Expected Hit Ratio" texts.

For that, I am using QGraphicsItem class to display squares on the QChart. code is given as you follow


<Header>

class MySqaure : public QGraphicsItem
{
public:
MySqaure();
QRectF boundingRect() const;

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};

<Cpp>

MySqaure::MySqaure()
{

}

QRectF MySqaure::boundingRect() const
{
return QRectF(0, 0, 100, 100);
}

void MySqaure::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rec = boundingRect();
QBrush brush (Qt::blue);
brush.setColor(Qt::green);

painter->fillRect(rec, brush);
painter->drawRect(rec);
}

how can I add MySquare to the Qchart view?

d_stranz
23rd May 2021, 16:02
QChartView inherits from QGraphicsView and therefore you can access the QGraphicsScene instance using QGraphicsView::scene(). Once you have the scene pointer, you add your own graphics items to the scene in the correct locations.