QCustomPlot Discussion and Comments

QCPItemRect on top of QCPColorMap disappearsReturn to overview

I am trying to draw a rectangle on top of a color map.
First I draw the colormap:

  QCPColorMap *colorMap = new QCPColorMap( ui->plot->xAxis, ui->plot->yAxis );  
  colorMap->setData(DEMmap);
 
  // add color scale to the right
  QCPColorScale *colorScale = new QCPColorScale(ui->plot);
  ui->plot->plotLayout()->addElement(0,1,colorScale);
  colorScale->setType(QCPAxis::atRight);
  colorMap->setColorScale(colorScale); 
  colorMap->setGradient(QCPColorGradient::gpJet);
  colorMap->rescaleDataRange();
  ui->plot->setInteraction(QCP::iRangeDrag, true);
  ui->plot->setInteraction(QCP::iRangeZoom, true);
  
  QCPMarginGroup *marginGroup = new QCPMarginGroup(ui->plot);
  ui->plot->axisRect()->setMarginGroup(QCP::msBottom | QCP::msTop, marginGroup);
  colorScale->setMarginGroup(QCP::msBottom | QCP::msTop, marginGroup);

The code for a rectangle comes after:

  QCPItemRect* rect=new QCPItemRect( ui->plot );
  
  rect->topLeft->setCoords(QPointF(0,100));
  rect->bottomRight->setCoords(QPointF(100,0));  
  // rect->topLeft->setCoords(topLeftIntersectionArea);
  // rect->bottomRight->setCoords(bottomRightIntersectionArea);
  
  //rect->setBrush(QBrush(QColor(200,100,100,100)));
  rect->setBrush(QBrush(QColor("floralwhite")));
  
  // set to correct subplot-grid..
  rect->setClipAxisRect(ui->plot->axisRect());
  // rect->setClipToAxisRect(true);
  
  rect->topLeft->setAxes( ui->plot->axisRect()->axis(QCPAxis::atBottom),
			  ui->plot->axisRect()->axis(QCPAxis::atLeft));
  rect->bottomRight->setAxes( ui->plot->axisRect()->axis(QCPAxis::atBottom),
			      ui->plot->axisRect()->axis(QCPAxis::atLeft));

Now, if I leave the fixed coordinates it works, but If I replace them with the dynamically calculated topLeftIntersectionArea and bottomRightIntersectionArea (see the lines commented out), the rectangle doesn't appear. What could be the issue here?

Regards

Seems like the values of topLeftIntersectionArea and bottomRightIntersectionArea are way off-screen or at least outside the clip axis rect?

Also: make sure you've set the correct coordinate type: QCPItemPosition::setType

Hi, the second tip was the right one:

    rect->topLeft->setType(QCPItemPosition::ptPlotCoords);
    rect->bottomRight->setType(QCPItemPosition::ptPlotCoords);

did the job. Lesson learned: to draw on a graph, one has to select its coordinates.
Thanks!