QCustomPlot Discussion and Comments

changing colormap values disable interaction scrollingReturn to overview

i took the example "colormap" and change the values for x,y,z.
i´ve used a "promote to" qwidget for the colormap visualization.
by clicking a button, the colormap should take a few values and visualize it.
If i do this one time everything is fine(->correct range, correct picture-zoom-function, correct intense-zoom-function, scroll function etc...) but if i do a second time, it seems like the colormap is inaktive.
The Zoom function is not working and it seems like the new picture is print over the old one.
Thanks for help

Found the solution: create a function"updateColormap" and "initializationColorMap" and set the with new allocatet variables visible for function "update"


#include <QMainWindow>
#include "qcustomplot.h"

namespace Ui { class MainWindow; }

class MainWindow : public QMainWindow

MainWindow(QWidget *parent = nullptr);

private slots:
void on_Start_clicked();

Ui::MainWindow *ui;
QCPColorMap *colorMap;
QCPMarginGroup *marginGroup;
QCPColorScale *colorScale;
#endif // MAINWINDOW_H

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)


delete ui;

void MainWindow::on_Start_clicked()
// configure axis rect:
ui->QC->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom); // this will also allow rescaling the color scale by dragging/zooming

// set up the QCPColorMap:
//QCPColorMap *colorMap = new QCPColorMap(ui->QC->xAxis, ui->QC->yAxis);
colorMap=new QCPColorMap(ui->QC->xAxis, ui->QC->yAxis);
int nx = 200;
int ny = 200;
colorMap->data()->setSize(nx, ny); // we want the color map to have nx * ny data points
colorMap->data()->setRange(QCPRange(-4, 4), QCPRange(-4, 4)); // and span the coordinate range -4..4 in both key (x) and value (y) dimensions
// now we assign some data, by accessing the QCPColorMapData instance of the color map:
double x, y, z;
for (int xIndex=0; xIndex<nx; ++xIndex)
for (int yIndex=0; yIndex<ny; ++yIndex)
colorMap->data()->cellToCoord(xIndex, yIndex, &x, &y);
double r = 3*qSqrt(x*x+y*y)+1e-2;
z = 2*x*(qCos(r+2)/r-qSin(r+2)/r); // the B field strength of dipole radiation (modulo physical constants)
colorMap->data()->setCell(xIndex, yIndex, z);

// add a color scale:
//QCPColorScale *colorScale = new QCPColorScale(ui->QC);
colorScale = new QCPColorScale(ui->QC);
ui->QC->plotLayout()->addElement(0, 1, colorScale); // add it to the right of the main axis rect
colorScale->setType(QCPAxis::atRight); // scale shall be vertical bar with tick/axis labels right (actually atRight is already the default)
colorMap->setColorScale(colorScale); // associate the color map with the color scale
colorScale->axis()->setLabel("Magnetic Field Strength");

// set the color gradient of the color map to one of the presets:
// we could have also created a QCPColorGradient instance and added own colors to
// the gradient, see the documentation of QCPColorGradient for what's possible.

// rescale the data dimension (color) such that all data points lie in the span visualized by the color gradient:

// make sure the axis rect and color scale synchronize their bottom and top margins (so they line up):
QCPMarginGroup *marginGroup = new QCPMarginGroup(ui->QC);
ui->QC->axisRect()->setMarginGroup(QCP::msBottom|QCP::msTop, marginGroup);
colorScale->setMarginGroup(QCP::msBottom|QCP::msTop, marginGroup);

// rescale the key (x) and value (y) axes so the whole color map is visible:

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])

QApplication a(argc, argv);
MainWindow w;
return a.exec();