Detecting QCandlestickSet in a QList<QGraphicsItem *>

  • Hi everyone,

    I am really having troubles to detect the QCandelstickSet elements after calling the next sentence:
    itemList = scene->items(rect, Qt::IntersectsItemShape, Qt::DescendingOrder);

    Of course, the QCandlestickSet elements are drawn in a Qchart in a QChartView, which has its corresponding scene.

    I tried the following but did not work:

    for (int i = 0; i < itemList.count(); ++i)
        QCandlestickSet *set = dynamic_cast<QCandlestickSet*>(;

    I was reading the Qt documentation but I think I am missing something. It says that it must be through the const int QGraphicsItem::UserType
    The lowest permitted type value for custom items (subclasses of QGraphicsItem or any of the standard items). This value is used in conjunction with a reimplementation of QGraphicsItem::type() and declaring a Type enum value. Example:

      class CustomItem : public QGraphicsItem
         enum { Type = UserType + 1 };
         int type() const
             // Enable the use of qgraphicsitem_cast with this item.
             return Type;

    Does that mean that I have to create my own QCandlestickSet class derived from QGraphicsItem?

    If someone knows how to do it, I would really appreciate an example.

    Thank you.

  • QCandlestickSet does not inherit QGraphicsItem so there's no way your cast can have success.

    The cast could work with CandlestickChartItem but it's a private class:

  • Does that mean that I have to create my own QCandlestickSet class derived from QGraphicsItem?

    I think so. Not sure, since I don't really understand what you are trying to do. scene->items() will give you a list of items. Nothing else is going to be in this list.

    This is the first time I hear about candleStickSet, so I'm not sure, but can't you do something like this?

    class myCandleStickItem: public QGraphicsItem{
    myCandleStickItem(QWidget *parent = Q_NULLPTR) ;
    QCandlestickSet getcandlestickSet() {return candlestickSet ;}
    void setCandleStickSet( QCandlestickSet newone) { candlestickSet  = newone ;}
    QCandlestickSet candlestickSet;

  • @Andeol
    Maybe I should have started saying that I have a QCandlestickSeries *series where I store financial data. Each single day in the series is stored in its corresponding QCandelstickSets *set. The series is added to a QChart chart and this one presented in a QChartView view.

    So far everything works since the date is drawn when I press the button. I even reimplemented the QChartView class to have my own zoom command. And that works also. This is the part of the reimplemented class with respect to the zoom:

    void ChartView::keyPressEvent(QKeyEvent *event)
    QRectF rect = chart()->plotArea();
    qreal adjustment = rect.width() / 2;

    switch (event->key())
    case Qt::Key_Up:

        //rect.setX(chart()->plotArea().x() + adjustment);
        rect.adjust(adjustment, 0, 0, 0);

    case Qt::Key_Down:

    As you can see, the zoom is only in the X axis.
    I would like to zoom the Y axis as well, but in such a way that it takes into account the maximum value of the different sets (QCandlestickSet) that fall inside the x-zoomed area. That is to say, I would like an autoscale in the Y axis to watch the sets as big as possible.

    To do that, I was thinking on something like:

    QGraphicsScene *scene = chart()->scene();
    QList<QGraphicsItem *> itemList;
    itemList = scene->items(rect, Qt::IntersectsItemShape, Qt::DescendingOrder);

    But this is not working, as you guys said before.

    Thank you very much for your help,

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.