QCustomPlot Discussion and Comments

Access a colormap from a different method.Return to overview

Hi all:
I create in one of the methods in the class a colormap in the following fashion:

void group_ex::onDataSent(QVector<double> range,QVector<double> loc,QVector<double> linenb,QVector<double> pointnb,QVector<double> data){
QCPColorMap *dataColorMap = new QCPColorMap(ui->dataPlot->xAxis,ui->dataPlot->yAxis);

In my class I have another method to capture the mouse movement over the plot and I want to display the colormap values in this way:
void group_ex::slotMouseMove(QMouseEvent *ev){
    double xc = ui->dataPlot->xAxis->pixelToCoord(ev->pos().x());
    double yc = ui->dataPlot->yAxis->pixelToCoord(ev->pos().y());
//    qDebug() << dataColorMap->data()->data(xc,yc); <-access colormap values in this method

But I can't acces the color map I created in the other method is there a way to access the colormap via the plot? Can the colormap be created in a more global context so it is available to all the class?
I tried to declare:
QCPColorMap *dataColorMap

but it needs the plot which is only accessible once the ui is ready. Graphs can be accessed via the plot anywhere using:

I was wondering if something similar could be done for the colormap? Any help would be greatly appreciated.
Best regards

The preferred method is to keep a (smart) pointer to the specific plottable – in your case the QCPColorMap – in a more global context, e.g. your plot manager class or main window or whatever. You initialize this pointer with 0 at construction of the plot manager/main window/etc. and fill it as soon as the color map is ready, e.g. when you set the QCustomPlot and its plottables up. I don't quite see what the problem is with "but it needs the plot which is only accessible once the ui is ready".

Alternatively you can access all plottables (not just graphs) with ui->dataPlot->plottable(i). To test for and cast to QCPColorMap, use qobject_cast<QCPColorMap*>

Thanks! :) Declaring the the colormap in a global context solved this problem!