QCustomPlot Discussion and Comments

How can I subclass QCPPlottableLegendItem and use it?Return to overview

QCPAbstractPlottable constructor call QCustomPlot's registerPlottable function , and then add plottable to legend, but addToLegend just use new QCPPlottableLegendItem, and because addToLegend call from QCPAbstractPlottable ctor, so can't use virtual function.

So anyone can help me ? I want subclass QCPPlottableLegendItem and use it.

Thanks. lancelot.

QCPAbstractPlottable::QCPAbstractPlottable(QCPAxis *keyAxis, QCPAxis *valueAxis)
{
    mParentPlot->registerPlottable(this);
}

bool QCustomPlot::registerPlottable(QCPAbstractPlottable *plottable)
{
    if (mAutoAddPlottableToLegend)
        plottable->addToLegend();
}

bool QCPAbstractPlottable::addToLegend(QCPLegend *legend)
{
    if (!legend->hasItemWithPlottable(this)) 
    {
         legend->addItem(new QCPPlottableLegendItem(legend, this));
         return true;
    }
}

I used the following method to achieve what I wanted.

QCPPie::QCPPie(QCPAxis *keyAxis, QCPAxis *valueAxis)
    : QCPAbstractPlottable(keyAxis, valueAxis)
{
    if (mParentPlot->autoAddPlottableToLegend() && mParentPlot->legend) 
    {
        mParentPlot->legend->removeItem(mParentPlot->legend->itemWithPlottable(this));
        mParentPlot->legend->addItem(new QCPPieLegendItem(mParentPlot->legend, this));
    }
}