QCustomPlot Discussion and Comments

[Suggestion] QCPSelectionDecorator that modifies the plot thicknessReturn to overview
September 5, 2025, 02:35
by Ironic Chloride

Hi,

I needed a way to show which QCPGraph is currently selected and I wanted to use some built-in mechanisms instead of setting the width manually. So I came up with the following idea. Since the selected graph is drawn using QCPSelectionDecorator I thought I could use that. Unfortunately, this class doesn't allow to modify the properties of the plottable, but just sets its own properties. If I want to change just the thickness, I can't, I have to change the whole QPen, that means also the colour. My application allows to modify the colour of the plottable in many places, and I wasn't feeling like modifying the decorator each time, so i came up with this:

class PlotThicknessSelectionDecorator : public QCPSelectionDecorator {
public:
 explicit PlotThicknessSelectionDecorator(float widthMultipler = 2.0) :
 m_SelectedWidthMultipler(widthMultipler) {}
 void setWidthMultiplier(float &multiplier) {
 m_SelectedWidthMultipler = multiplier;
 }
 float widthMultiplier() {
 return m_SelectedWidthMultipler;
 }
 // The declaration of this method in qcustomplot.h was modified to be virtual
 void applyPen(QCPPainter *painter) const override {
 QPen pen = mPlottable->pen();
 pen.setWidthF(pen.widthF() * m_SelectedWidthMultipler);
 painter->setPen(pen);
 }
private:
 float m_SelectedWidthMultipler = 2.0;
};

This requires the modification in qcustomplot.h: virtual void applyPen(QCPPainter *painter) const; // add virtual
Now the only thing that is required is to set the decorator when the graph is created, and when it is selected the new thicker pen will be used.
So the question ad suggestion is: Are there any drawbacks of doing it like this? Maybe more methods in QCPSelectionDecorator should be made virtual, so that this class could be subclassed to create own fancy decorators?

AltStyle によって変換されたページ (->オリジナル) /