2017-10-12 999 views
0

我有一個問題,我必須從某些來源繪製光線。來源強度應該是最強的,並且應該隨距離而減少,這是我的x軸。如果我使用藍色繪製光線而不是它應該是淺藍色在原點,應該與距離變暗。如何改變Qcustomplot中曲線顏色相對於x軸的強度?

我附加QCpcurve QCustomplot。

有兩個向量說X和Y,我有繪製

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

問題是,如何改變顏色強度隨着距離的增加。

請幫忙

+0

顯示你的代碼。 – eyllanesc

+0

你可以顯示你想要得到的圖像。 – eyllanesc

+0

@eyllanesc我編輯了我的帖子。 – Aman

回答

2

您可以通過顯示您想要的外觀來設置顏色漸變爲QPen。

QPEN :: QPEN(常量QBrush &刷,QREAL寬度,QT :: PenStyle風格= Qt的:: SolidLine,Qt的:: PenCapStyle帽= Qt的:: SquareCap,Qt的:: PenJoinStyle 加入= 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(); 
} 

enter image description here

+0

@eyllansec感謝哥們。你的解決方案是完美的 – Aman