[Visual Studio] Qt's real-time drawing curve function, using C++ language, cooperates with Qt to develop serial communication interface

Knowledge is not alone, it must be systematic. More my personal summary and related experience can be found in this column: Visual Studio .

Combat background: A serial port receiving interface was made to receive the transmitted signal. However, it is too monotonous to display with numbers alone, and it needs to be displayed with graph lines.

Strategic Goal: Kill it.

Tactical route: You can use Qt Charts for Qt drawing, first understand some basic knowledge about Qt Charts, and then integrate it into my project according to your real-time dynamic curve needs.

1. Implement the simplest line chart

Effects first, explanations later. The operation steps and effects are as follows:

documentGUI.h

insert image description here

insert image description here

documentGUI.cpp

insert image description here

The final running effect:
insert image description here


Possible bug: C1083

Just refer to this article to solve it: 【Visual Studio】Error C1083, use C++ language, cooperate with Qt to develop serial communication interface .


knowledge behind

The relevant classes provided by Qt are:

insert image description here

Forget it, there are a total of 48 classes. You don’t need to use all of them to realize a chart. According to the chart you need to implement, you only need to use a few of them. These classes can be roughly divided into the following categories:

  • QChartView Class: An area used to display charts, or understood as a canvas, can realize all charts supported by Qt Chart on QChartView.

QChartView -> QGraphicsView -> QAbstractScrollArea -> QFrame -> QWidget

  • QChart Class: QChart is a QGraphicsWidget, which can be displayed on QGraphicsScene and used to manage data, legends, coordinate axes, etc. in the chart.

QChart -> QGraphicsWidget -> QGraphicsObject and QGraphicsLayoutItem QGraphicsObject <-- QObject and QGraphicsItem

  • QLineSeries ClassThis is the type of chart, others are QSplineSeries, QAreaSeries, QScatterSeriesand so on.

2. Configure the display area of ​​the chart chart

The display position of the table made by the above steps is displayed in the entire interface frame, and other contents are covered, which is definitely not possible.

Next, I will record the way I think is good after comparing it from the Internet. The net effect is to limit the graph to a certain area. Results as shown below.

insert image description here

documentGUI.ui

Here I am using the method of adding Widgeta .

insert image description here

insert image description here

insert image description here

insert image description here

documentGUI.cpp

insert image description here


Possible bug: C2653

Just refer to this article to solve it: [Visual Studio] Report error C2653, use C++ language, and cooperate with Qt to develop serial communication interface .


3. Add XY axis and modify the display range

Before realizing the dynamic curve, we must first have the function of modifying the display range of the coordinate axis. Otherwise, when more and more data is transmitted, and the display range of the coordinate axis does not change in time, the data cannot be displayed normally.

insert image description here

documentGUI.h

insert image description here

documentGUI.cpp

insert image description here

4. Use the current system time as the X-axis coordinate

documentGUI.ui

insert image description here

documentGUI.h

This qdatetimeaxis.his done .

insert image description here

insert image description here

documentGUI.cpp

insert image description here

5. Dynamic curve

Dynamic curve, as the name suggests, is to make the curve move. In fact, after the above steps, it only takes two sentences to make the program move.

insert image description here

I wrote these two sentences into my serial port receiving program, that is, after receiving the data, I will update the chart. You can put it in a suitable position according to your own needs, so that its update mechanism can better meet your project needs.

Finally, put a picture of the final effect of this small project. Further down, it involves some laboratory confidential data, so it is inconvenient to continue to display.

Please add a picture description

Ref.

  1. 1. Qt Charts
  2. QtCharts programming notes: VS2019+Qt Charts 5.15.1 environment configuration
  3. Qt GUI development (1) - simple use of Qt Chart
  4. QChart dual Y-axis updates the graph in real time (the abscissa is the current time)
  5. QChart draws a proportion chart, but takes the time axis as the X axis

Guess you like

Origin blog.csdn.net/weixin_36815313/article/details/131335145