Using Qt to draw a graph of sin (x)

I am experimenting with sine wave graph drawing methods.

My widget only expects the transfer of multiple data points. I have to match these data points with a sine curve line:

Sin (x)

So far I have tried several methods using QPainterPath .

  • QPainterPath :: lineTo. I tried to use this function to build a curve by taking my data and creating so many points that they have that the line is a little smoothed. It's a little too computationally intense, but I feel.
  • QPainterPath :: cubTo - from what I put together in RTFM, this is the best way. The only problem is that I'm not sure how to build my control points in places where it will consistently and programmatically smooth the curve the way I want it. I could not get the desired result using this function.

After doing some googling, I came across several forum posts that used Qwt to build a curve. It would be great if I could use Qwt, but this is not an option, as I am limited to using Qt only.

Does anyone have any useful feedback / suggestions?

+9
qt
source share
3 answers

I am currently doing a very similar picture with the baud-mapping of a parametric equalizer (a long line with several curves). The way I do it (pseudo-style):

qreal yCoords[GRAPH_WIDTH]; ... QPainter Painter(this); Painter.setRenderHint(QPainter::Antialiasing, true); //Painter.setRenderHint(QPainter::HighQualityAntialiasing, true); //opengl specific for(int xCoord = 0; xCoord < GRAPH_WIDTH; x++) Path.lineTo(QPointF(xCoord, yCoord[xCoord])); ... Painter.drawPath(Path); 

The combination of setRenderHint calls and drawing lines with QPointF (i.e. two qreal ) and not QPoint (two int ) makes the line very smooth.

We use this on an SBC running Ubuntu, and we get redraw timeouts (including all the complex math to get points first) ~ 80 ms for a 600x300 pixel graph. Initial tests show that providing opengl rendering reduces this to ~ 8 ms (obviously, an intensive processor task is a picture with antialiasing), so if you can do this, I think this solution will work for you.

+7
source share

QCustomPlot is a free and easy to use class that can be found on the Internet. It may be better for what you want to do.

+3
source share

https://www.qcustomplot.com/index.php/tutorials/basicplotting

as simple as that, I suggest using QCustomPlot to build a chart in a Qt c ++ environment

0
source share

All Articles