Search code examples
c++qtqcustomplotqcolorqpen

how to change the intensity of curve colour in Qcustomplot with respect to x-axis?


I have a problem where I have to plot a ray from some source.At source the intensity should be strongest and should decrease with distance which is my xaxis.If I am using blue colour to plot my ray than it should be light blue at origin and should darken with distance.

I have attached a QCpcurve to QCustomplot.

There are two vectors say X and Y which I have to plot

Curve.setpen(blue);
Curve.setdata(X,Y);

Problem is that how to change the colour intensity as distance increases.

Please help


Solution

  • You can set a color gradient to QPen by displaying the look you want.

    QPen::QPen(const QBrush &brush, qreal width, Qt::PenStyle style = Qt::SolidLine, Qt::PenCapStyle cap = Qt::SquareCap, Qt::PenJoinStyle join = Qt::BevelJoin)

    Constructs a pen with the specified brush, width, pen style, cap style and join style.

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QCustomPlot *customplot = new QCustomPlot;
        customplot->setWindowTitle("Gradient Color");
        customplot->resize(640, 480);
        QCPCurve curve(customplot->xAxis, customplot->yAxis);
        QVector<double> x, y;
        for(int i=0; i < 1000; i++){
            double x_ = qDegreesToRadians(i*1.0);
            x << x_;
            y << qCos(x_)*qExp(-0.2*x_);
        }
        customplot->xAxis->setRange(0, qDegreesToRadians(1000.0));
        customplot->yAxis->setRange(-1, 1);
    
        QLinearGradient gradient(customplot->rect().topLeft(), customplot->rect().topRight());
        gradient.setColorAt(0.0, QColor::fromRgb(14, 11, 63));
        gradient.setColorAt(1.0, QColor::fromRgb(58, 98, 240));
        QPen pen(gradient, 5);
        curve.setPen(pen);
    
        curve.setData(x, y);
        customplot->show();
    
        return a.exec();
    }
    

    enter image description here