How to plot graphs using Qcustomplot in the main thread?

Asked

Viewed 443 times

1

I would like to know how to plot the serial port data in the Qcustomplot library to keep updating. I thank you for understanding.

void serialThread::run(){

mutex.lock(); 

 if(serial->waitForReadyRead(1)){

  QByteArray data = serial->readAll();

     qDebug () << data;

 }
   mutex.unlock();
 }
  • What is the run method? How does this relate to the title?

  • I need to plot graphics via thread, only I don’t know how to use the main thread, because the Qcustomplot library doesn’t allow plotting of graphics outside the main thread and I don’t know how to plot the graphics in the main thread!

  • 1

    Now, what did you try to do and what was the problem that happened? Merely calling the library does not plot on the main thread? Did something go wrong? -- I’m sorry to be boring, but for you the problem is clear, but is not passing the problem clearly to us. I really still don’t understand what you want.

  • 1

    @Netinhosantos It would be nice to post the code you have so far, otherwise there is a guessing game to help you. As the question stands, it is not possible to understand in which of the stages of development is its difficulty. Note that with more information we can help better and more objectively. To add details, just click on [Edit] just below the text of the original question.

1 answer

5

Using Qcustomplot to plot:

The class QCustomPlot inherits from QWidget and so always operates on the main thread. An example taken directly from documentation:

// generate some data:
QVector<double> x(101), y(101); // initialize with entries 0..100
for (int i=0; i<101; ++i)
{
  x[i] = i/50.0 - 1; // x goes from -1 to 1
  y[i] = x[i]*x[i]; // let's plot a quadratic function
}
// create graph and assign data to it:
customPlot->addGraph();
customPlot->graph(0)->setData(x, y);
// give the axes some labels:
customPlot->xAxis->setLabel("x");
customPlot->yAxis->setLabel("y");
// set axes ranges, so we see all data:
customPlot->xAxis->setRange(-1, 1);
customPlot->yAxis->setRange(0, 1);
customPlot->replot();

Where the customPlot is a QCustomPlot previously created.


On how to plot out the main thread:

For, in my opinion, a design flaw of the library, the class responsible for displaying the graph on the screen is the same responsible for rendering the graph itself. That is, at the same time that it performs intensive processing, it inherits from QWidget, that cannot be created outside the main thread.

That way you have some not so good options:

  1. Create an external program that receives parameters on the command line and then render the chart using the widget. Note that you don’t need to call the function exec() application, then this process ends immediately after rendering and saving the chart. Then, from your main program, run this by passing the parameters you need and waiting for termination. You can use QProcess for this. Note that unfortunately we will still need to create a QApplication there, causing the loading of the entire Qt display structure.

  2. Take a more aggressive attitude and edit the library by removing dependency from the QWidget. It works, but it’s not quite ideal and will cause a lot of work unrelated to your program itself. In addition to the risk of causing new bugs, and having a whole rework when releasing new versions.

If you are going to take Path 2, I suggest you contribute your changes to their repository, improving the library as a whole and taking the weight out of you maintaining the code.

  • William, my question is how could I use the main thread to plot the graphics since Qcustomplot does not allow plotting outside of it.

  • Or I’m seriously thinking about looking for another way for this task. The serial port is already reading and configuring the parameters. The program has to connect to Arduino, read the sensor data and plot the graphics.

  • "how could I use the main thread to plot the graphics since Qcustomplot does not allow plotting outside of it". Do you want or do not want to use the main thread?

  • Rsrsrsrsrs Yes!

  • 2

    Er. "Yes" means you want to use or "Yes" means you don’t want to use?

  • @Please edit and clarify your question with exactly what you want to do. It is not clear your intention.

  • Yes! I want to use the main thread

  • @So what’s the problem? The library already uses the main thread by default. Again: edit and make your question clear. Avoid answering here in the comment. Edit the question.

  • @Guilhermebernal half off what I will ask: if you create the graph in another thread, and then move to the main thread and re-family the object, doesn’t it work? Would be the methods moveToThread and setParent. I don’t know how to QCustomPlot works, but with other controls it works.

  • @Viníciusgobboa.deOliveira unfortunately not because QCustomPlot inherits from QWidget, that cannot leave the main thread.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.