Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. is it possible to set the pen color to a gradient?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 5.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    misoran
    wrote on last edited by misoran
    #1

    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->addGraph();
    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->addGraph();
    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);
    ui->customPlot->graph()->rescaleAxes(true);
    // give the axes some labels:
    ui->customPlot->xAxis->setLabel("time");
    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->replot();
    ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    
    
    ui->customPlot->graph()->setLineStyle(QCPGraph::lsLine);
    ui->customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
    ui->customPlot->graph(1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
    
    1 Reply Last reply
    0
    • Chris HennesC Offline
      Chris HennesC Offline
      Chris Hennes
      wrote on last edited by
      #2

      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!).

      Chris Hennes, Pioneer Library System

      1 Reply Last reply
      1
      • M Offline
        M Offline
        misoran
        wrote on last edited by
        #3

        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.setCenter(drawingRect.center());
        gradient.setAngle(90);
        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

        1 Reply Last reply
        0
        • IMAN4KI Offline
          IMAN4KI Offline
          IMAN4K
          wrote on last edited by IMAN4K
          #4

          @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.setCenter(rect().center());
          		gradient.setAngle(90);
          		gradient.setColorAt(1.0, Qt::black);
          		gradient.setColorAt(0.0, palette().background().color());
          
          		auto p = QPen(gradient, 4.0);
          		painter.setPen(p);
          		painter.drawLine(line);
          
          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved