QCustomPlot Discussion and Comments

bug fix and patch for QCPItemText::draw()Return to overview

1. Bug in QCPItemText::draw(QCPPainter *painter)
Draw doesnt restore oldTransform like is done in other draw functions resulting in glitches when rendering on multilayer
2. Check visibility of QCPItemText, if it isn't visible, don't draw it

 /* inherits documentation from base class */
 void QCPItemText::draw(QCPPainter *painter)
+  if (!visible()) { return; }
   QPointF pos(position->pixelPosition());
   QTransform transform = painter->transform();
+  QTransform oldTransform = transform;
   transform.translate(pos.x(), pos.y());
   if (!qFuzzyIsNull(mRotation))
@@ -29040,6 +29058,7 @@
     painter->drawText(textRect, Qt::TextDontClip|mTextAlignment, mText);
+    painter->setTransform(oldTransform);

see QCPLayer::draw

void QCPLayer::draw(QCPPainter *painter)
foreach (QCPLayerable *child, mChildren)
if (child->realVisibility())
painter->setClipRect(child->clipRect().translated(0, -1));

Hi marek,

as lancelot has indirectly pointed out by posting code, the individual item (and plottable) draw functions aren't given that responsibility in QCustomPlot, that would just lead to many errors by forgetting the restore the painter or checking for visibility. All this boilerplate stuff is done by the QCustomPlot caller, i.e. QCPLayer::draw().

Please let me know which behavior you have encountered that made you think there was something wrong with QCPItemText::draw.