2

我有一个问题,我必须从某个源绘制一条射线。在源处,强度应该最强,并且应该随着距离(即我的 xaxis)而减小。如果我使用蓝色来绘制我的射线,那么它应该在原点为浅蓝色并且应该随着距离变暗。

我已将 QCpcurve 附加到 QCustomplot。

有两个向量说 X 和 Y 我必须绘制

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

问题是如何随着距离的增加改变颜色强度。

请帮忙

4

1 回答 1

4

您可以通过显示您想要的外观为 QPen 设置颜色渐变。

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

构造具有指定画笔、宽度、笔样式、帽样式和连接样式的笔。

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();
}

在此处输入图像描述

于 2017-10-12T04:12:12.383 回答