QCustomPlot Discussion and Comments

Feature request: Align background image to top rightReturn to overview

In our application we added a water mark to the graphs plotted with QCustomPlot. We wanted it to be in the top right instead of the top left. As this seemed to be unsupported we implemented the functionality. It would be nice to see this feature be implemented upstream.

This is the git diff of our reference implementation, it supports scaled and unscaled images:

diff --git a/qcustomplot/qcustomplot.cpp b/qcustomplot/qcustomplot.cpp
index 51722e248..19c315c15 100644
--- a/qcustomplot/qcustomplot.cpp
+++ b/qcustomplot/qcustomplot.cpp
@@ -12873,6 +12873,7 @@ QCustomPlot::QCustomPlot(QWidget *parent) :
   mNoAntialiasingOnDrag(false),
   mBackgroundBrush(Qt::white, Qt::SolidPattern),
   mBackgroundScaled(true),
+  mBackgroundAlignRight(false),
   mBackgroundScaledMode(Qt::KeepAspectRatioByExpanding),
   mCurrentLayer(0),
   mPlottingHints(QCP::phCacheLabels|QCP::phImmediateRefresh),
@@ -13483,6 +13484,17 @@ void QCustomPlot::setBackgroundScaled(bool scaled)
 }
 
 /*!
+  Sets whether the viewport background pixmap shall be aligned to the top right of the viewport
+  instead of the top left.
+
+  \see setBackground
+*/
+void QCustomPlot::setBackgroundAlignRight(bool alignRight)
+{
+  mBackgroundAlignRight = alignRight;
+}
+
+/*!
   If scaling of the viewport background pixmap is enabled (\ref setBackgroundScaled), use this
   function to define whether and how the aspect ratio of the original pixmap is preserved.
   
@@ -15059,10 +15071,26 @@ void QCustomPlot::drawBackground(QCPPainter *painter)
       scaledSize.scale(mViewport.size(), mBackgroundScaledMode);
       if (mScaledBackgroundPixmap.size() != scaledSize)
         mScaledBackgroundPixmap = mBackgroundPixmap.scaled(mViewport.size(), mBackgroundScaledMode, Qt::SmoothTransformation);
-      painter->drawPixmap(mViewport.topLeft(), mScaledBackgroundPixmap, QRect(0, 0, mViewport.width(), mViewport.height()) & mScaledBackgroundPixmap.rect());
+      if(mBackgroundAlignRight)
+      {
+        const int cutoff = std::max(0, scaledSize.width() - mViewport.width());
+        painter->drawPixmap(mViewport.topRight() - QPoint(scaledSize.width() - cutoff, 0), mScaledBackgroundPixmap,
+            QRect(cutoff, 0, mViewport.width(), mViewport.height()) & mScaledBackgroundPixmap.rect());
+      } else
+      {
+        painter->drawPixmap(mViewport.topLeft(), mScaledBackgroundPixmap, QRect(0, 0, mViewport.width(), mViewport.height()) & mScaledBackgroundPixmap.rect());
+      }
     } else
     {
-      painter->drawPixmap(mViewport.topLeft(), mBackgroundPixmap, QRect(0, 0, mViewport.width(), mViewport.height()));
+      if(mBackgroundAlignRight)
+      {
+        const int cutoff = std::max(0, mBackgroundPixmap.width() - mViewport.width());
+        painter->drawPixmap(mViewport.topRight() - QPoint(mBackgroundPixmap.width() - cutoff, 0), mBackgroundPixmap,
+            QRect(cutoff, 0, mViewport.width(), mViewport.height()) & mBackgroundPixmap.rect());
+      } else
+      {
+        painter->drawPixmap(mViewport.topLeft(), mBackgroundPixmap, QRect(0, 0, mViewport.width(), mViewport.height()) & mBackgroundPixmap.rect());
+      }
     }
   }
 }
diff --git a/qcustomplot/qcustomplot.h b/qcustomplot/qcustomplot.h
index 3b046e5d0..cb5e755ac 100644
--- a/qcustomplot/qcustomplot.h
+++ b/qcustomplot/qcustomplot.h
@@ -3594,6 +3594,7 @@ class QCP_LIB_DECL QCustomPlot : public QWidget
   Q_PROPERTY(QRect viewport READ viewport WRITE setViewport)
   Q_PROPERTY(QPixmap background READ background WRITE setBackground)
   Q_PROPERTY(bool backgroundScaled READ backgroundScaled WRITE setBackgroundScaled)
+  Q_PROPERTY(bool backgroundAlignRight READ backgroundAlignRight WRITE setBackgroundAlignRight)
   Q_PROPERTY(Qt::AspectRatioMode backgroundScaledMode READ backgroundScaledMode WRITE setBackgroundScaledMode)
   Q_PROPERTY(QCPLayoutGrid* plotLayout READ plotLayout)
   Q_PROPERTY(bool autoAddPlottableToLegend READ autoAddPlottableToLegend WRITE setAutoAddPlottableToLegend)
@@ -3633,6 +3634,7 @@ public:
   double bufferDevicePixelRatio() const { return mBufferDevicePixelRatio; }
   QPixmap background() const { return mBackgroundPixmap; }
   bool backgroundScaled() const { return mBackgroundScaled; }
+  bool backgroundAlignRight() const { return mBackgroundAlignRight; }
   Qt::AspectRatioMode backgroundScaledMode() const { return mBackgroundScaledMode; }
   QCPLayoutGrid *plotLayout() const { return mPlotLayout; }
   QCP::AntialiasedElements antialiasedElements() const { return mAntialiasedElements; }
@@ -3654,6 +3656,7 @@ public:
   void setBackground(const QPixmap &pm, bool scaled, Qt::AspectRatioMode mode=Qt::KeepAspectRatioByExpanding);
   void setBackground(const QBrush &brush);
   void setBackgroundScaled(bool scaled);
+  void setBackgroundAlignRight(bool alignRight);
   void setBackgroundScaledMode(Qt::AspectRatioMode mode);
   void setAntialiasedElements(const QCP::AntialiasedElements &antialiasedElements);
   void setAntialiasedElement(QCP::AntialiasedElement antialiasedElement, bool enabled=true);
@@ -3777,6 +3780,7 @@ protected:
   QPixmap mBackgroundPixmap;
   QPixmap mScaledBackgroundPixmap;
   bool mBackgroundScaled;
+  bool mBackgroundAlignRight;
   Qt::AspectRatioMode mBackgroundScaledMode;
   QCPLayer *mCurrentLayer;
   QCP::PlottingHints mPlottingHints;

But you could just create a qcpitemPixmap and put it on the bottom layer...