0

In my UI, I have a QCustomPlot and I have generatePlot function which uses from this QCustomPlot and runs in a new thread as follows:

future = QtConcurrent::run(this, &MainWindow::generatePlot);

In generatePlot function, I create QCPbars like this:

QCPBars *bar = new QCPBars(customPlot->xAxis, customPlot->yAxis);

But, I get this error:

QObject: Cannot create children for a parent that is in a different thread.

I think that's because the thread of customPlot is diffrenet from bar. How can I solve it?

Azeem
  • 11,148
  • 4
  • 27
  • 40
  • You should create a Worker class for this processing and not do that in a MainWindow method. Also this code is probably not what you have, it should not compile : `QCPBars bar = new QCPBars` please give more details. – ymoreau Jan 19 '18 at 10:30
  • @ymoreau thanks, yes, I'm sorry, I changed my code to be here more obviously. I modified it. Original command was: `QCPBars *bars[plotsNum_+1];` and in the `for` `bars[i] = new QCPBars(customPlot->xAxis, customPlot->yAxis);` Did you mean that I should create a new class which has inherited from `QThread` –  Jan 19 '18 at 10:43
  • No, inheriting from `QThread` is not the recommended practice, `QtConcurrent` is probably the best choice in your case, keep this. But you could create a dedicated class that contains the method `generatePlot` and the `bars` array and any related data, so you do not mess up with `MainWindow` data-members and methods, you isolate the code to run concurrently. – ymoreau Jan 19 '18 at 11:04
  • About your error message, it is not allowed to create a child `QObject` in a different thread-context than its parent, you will need to post more code, where could you set a parent to a new object in your theaded-code ? – ymoreau Jan 19 '18 at 11:05
  • @ymoreau But I have a `QCustomPlot` in one of my UI tabs, which I have to use that in `generatePlot` function, so in that dedicated class as you mentioned I have to get that `QCustomPlot` and this problem will happen again... –  Jan 19 '18 at 11:15
  • Please edit et explain your exact design, what objects and parenting you have – ymoreau Jan 19 '18 at 12:39
  • @ymoreau thanks, let me explain mush more: I have a pushbutton that: `on_plot_pushButton_clicked` function has following code in it: `future = QtConcurrent::run(this,&MainWindow::generatePlot);`. In `MainWindow::generatePlot` I have the following code: `GenPlot::getInstance()->generatePlot(ui->plot_widget);` which `ui->plot_widget` is `QCustomPlot` and in `GenPlot::generatePlot` I have `QCPBars` and so on... Now as a trick I use `ui->plot_widget=GenPlot::getInstance()->generatePlot()` and return `QcustomPlot` from `GenPlot::generatePlot` and replace with `ui->plot_widget` with no error –  Jan 19 '18 at 15:47
  • @ymoreau But my `ui->plot_widget` does not update even though I use `QCustomPlot::replot()` or `QCustomPlot::update()`. Why not update the `ui->plot_widget`? –  Jan 19 '18 at 15:54
  • Put the info in the question please ! The purpose is to make it clear for everyone who might read it, without reading the comments. – ymoreau Jan 19 '18 at 16:45
  • @ymoreau thank you, I created a new simpler question here: [https://stackoverflow.com/questions/48345789/qt-qcustomplot-parent] If you can't answer that, I edit this post and post my real code here. Thanks again... –  Jan 19 '18 at 16:51
  • 1
    You could show the code of the method that takes a long time, maybe you are implementing it in an incorrect way.The solution is not always to use a new thread, that is to abuse the resources. – eyllanesc Jan 19 '18 at 17:20

0 Answers0