Important: Please read the Qt Code of Conduct -

is it possible to set the pen color to a gradient?

  • I have been receiving issues dealing with line gradient. I am trying to plot the points from an array and using the pen to have a line gradient.
    No matter where i look I can't seem to find a way to set the pen to a line gradient. I have found many ways of setting the backgrounds to a gradient.
    My question would be if it was possible to set up a pen gradient. Below is a piece of my code

    // create graph and assign data to it:
    ui->customPlot->graph(0)->setPen(QPen(Qt::blue)); // line color blue for first graph
    													//ui->customPlot_7->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // first graph will be filled with translucent blue
    ui->customPlot->graph(1)->setPen(QPen(Qt::red)); // line color red for second graph
    ui->customPlot->graph(0)->setData(x0, y0);
    ui->customPlot->graph(1)->setData(x0, z0);
    // give the axes some labels:
    ui->customPlot->yAxis->setLabel("Y Velocity");
    // set axes ranges, so we see all data:
    // ui->customPlot->xAxis->setRange(0, 11);
    //ui->customPlot->yAxis->setRange(0, 3);
    ui->customPlot->yAxis->scaleRange(1.1, ui->customPlot->yAxis->range().center());
    ui->customPlot->xAxis->scaleRange(1.1, ui->customPlot->xAxis->range().center());
    ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    ui->customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
    ui->customPlot->graph(1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));

  • QPen has a constructor that takes a brush:

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

    So you can set up that brush with a gradient and pass it. I'm not sure it will give quite the effect you are looking for, though, the gradient is still an area gradient, it doesn't follow distance along the line (I don't know if that sentence makes any sense!).

  • so when im setting up the QPen how would set up the brush with a gradient? I'm saying this because i see brush set up all the time but never the pen. Do you mean like this example:
    ConicalGradient gradient;
    gradient.setColorAt(0, QColor(178, 255, 246));
    gradient.setColorAt(1, QColor(5, 44, 50));

    how do i set it up as the pen. I see it sets it up as an image, but my issues deal with setting it up as a pen. These set ups always seem to stay with a shape or the background but never the actual pen

  • @misoran
    You just simply want to setup QPen ?

                    QPainter painter(this);
    		auto line = QLineF(QPointF(rect().x() + 5, rect().center().y()), QPointF(rect().width() - 10, rect().center().y()));
    		QConicalGradient gradient;
    		gradient.setColorAt(1.0, Qt::black);
    		gradient.setColorAt(0.0, palette().background().color());
    		auto p = QPen(gradient, 4.0);

Log in to reply