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:
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.
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.
What is the run method? How does this relate to the title?
– Guilherme Bernal
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!
– Netinho Santos
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.
– Guilherme Bernal
@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.
– Bacco