B
B
Bogdan2015-05-14 00:19:07
Qt
Bogdan, 2015-05-14 00:19:07

How to draw a graph of a function using standard Qt tools?

I need to plot a function using standard Qt tools.
I decided to try to draw on QGraphicsView (since you still need to count the number of points, and QPainter draws on the entire window, as I understand it). I filled the vector with QPointF points, created a class from QGraphicsItem, in the Paint function, tried to draw this graph on QPainter using the drawLines function. Then I added this item to the scene, launched the program, pressed the "Draw" button and it crashed. Then, I tried simply using scene-> addLine to connect the points of that very vector, as a result, something similar to a curved triangle is drawn, but it should be something cosine. I don't know how else to do this without resorting to Qwt and QCustomPlot, and if it's even possible.
Upd.:I redid the first option a little, the program worked, but it also gives out some kind of heresy
8de6f32481.png

Answer the question

In order to leave comments, you need to log in

5 answer(s)
E
EXL, 2015-05-14
@EXL

The book "Qt 4: GUI Programming in C++, 2nd revised edition, Jasmine Blanchet, Mark Summerfield, 2008" discusses a simple example of a Plotter widget that can be used to draw graphs. The example is well documented and described in the book, it will not be difficult for you to figure it out.
Example source code (which is suitable for both Qt 4 and Qt 5).
Good luck!

D
DancingOnWater, 2015-05-14
@DancingOnWater

At the time I was doing this, I even wrote down an article on Habré: habrahabr.ru/post/182142 . You can study https://github.com/DancingOnWater/GraphicsScenePlot

Y
Yaroslav, 2015-05-14
@torwig

You can simply draw on the widget using QPainter (in paintEvent) without any QGraphicsView, etc.

S
Stanislav Makarov, 2015-05-14
@Nipheris

Studio code. Everything can be drawn and in paintEvent, as torwig has already suggested to you , there is a suspicion that the problem is in the calculations. To get an adequate picture, you need to correctly scale the results of calculating the function in points and change the coordinate system. So first check manually whether the numbers are adequate or not. Then you override paintEvent in your WidgetForGraphics class, create a QPainter in it, and immediately draw with it, it has a huge number of drawing methods. Here is the simplest example: doc.qt.io/qt-4.8/qpainter.html#details . Do without any scenes and interactive elements. Do not complicate until you figured out exactly where in the ALREADY WRITTEN code the problem is. "On its own" won't work.
PS If something crashes, deal with it separately, most likely you have problems with the lifetime of objects that we manipulate (create/delete incorrectly). The specificity of the pros is such that you need to follow this, otherwise you will quickly bury yourself in spaghetti from unusable code.

A
Antony, 2015-05-14
@RiseOfDeath

The Enterprise version of Qt has a dedicated graphics library. In the community, alas, only third-party libraries or self-made (QPainter or drawing through opengl).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question