Congratulations to our 2022 Qt Champions!


  • hello everyone.
    i gotta say that i'm a noobie around here, so i ask for a huge help.
    i need to do a program that plots a real time graphic on a X-Y axis. the x axis needs to run constantly while the program is opened for every second. and the y axis needs to aquire a random integer with low and high limits (for example, 20 to 95) and plot that integer for every second, or milisecond linked to x axis.
    i know that is possible with "q custom plot" and it needs to be done with it but i dunno how to implement that. can i use the qtimer class to generate the aleatory number while the program is running in real time?
    would u please tell me a example of that implementation?

  • Hi,

    first of all welcome to dev net. However, I guess CAPTIAL TITLES are not very well received here in the forums. I suggest you change it to attakt more answers.

    QCustomPlot is the right call here. I recently implemented such a realtime plot to plot EEG data on my own. I'll post the most important sections here to help you get on track.

    The general idea is to have an QTimer running at 40 Hz which triggers the computation (and update) of the shift of the x-axis each time called. Data is added whenever it arrives with no regard if the corresponding x-axis section is yet/still plotted. This way the plot runs smoothly. The downside is, that x-axis shift and incoming data are decoupled. Hence, data has to come at a very constant rate (which is the case for EEG data) or the x-axis section plotted might move into areas where there is not data (yet/anymore).

    From my .h file:
    QCustomPlot plot;
    QTimer updateTrigger;

    updateTrigger.setInterval(25); //40Hz
    timeOfFirstData = QTime::currentTime(); //needed to compute the x-axis section to be plotted.

    the Update slot:
    QTime currentTime = QTime::currentTime();
    double timeSinceStart = timeOfFirstData.msecsTo(currentTime)/1000.0;

    timeSinceStart contains the msecs since the start of the display and hence the right most point on the x-axis to be plotted. Sampling rate is the number of data point that occur per second (e.g. 256 in my case). numSamplesToDisplay is the number of data points I want to display (e.g. 2048 if I wanted to plot a window of 8 seconds).

    The function in which new data arrives:
    plot.graph(0)->removeDataBefore(numberOfDataPoint - (numSampleToDisplay*1.2))/samplingRate);

    If you have as the 1141st value you want to plot the value 45.8 in the code above dataPoint would be 45.8 and numberOfDataPoint would be 1141.

    I hope it helps. If there are question to that code. Please ask!


Log in to reply