Qt World Summit: Register Today!

How to use qgraphicsitem in qtchart?

  • Hi,

    I want to display various custom shapes, image and lines in qtchart.
    QChart does not inherit from QGraphicsScene, how do I use qgraphicitem ?
    And the use coordinates of qgraphicsitem want to use the x, y coordinate system of qchart.


  • Lifetime Qt Champion

    There is not something like
    app->chart()->scene() ?

  • QChart is descendant of QGraphicsWidget which in term is descendant of QGraphicsItem
    So it's meant to be placed in a QGraphicsScene within QGraphicsView::scene().

    You can use QGraphicsView to render your QChart as well as any other customized QGraphicsItem.
    You have first to subclass QGraphicsView, within the constructor, you can call the virtual method scene() .
    Which would you able add QCharts and other QGraphicsItem objects. i.e. scene()->addItem(new QChart) or scene()->addItem(new QGraphicsItem).

    And for the coordinates of QChart, I am not quite sure, but you might subclass a QGraphicsItem parent it to the QChart and override it's paint method

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

    to draw custom shapes within the QCharts coordinate system using QPainter.

Log in to reply