Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello CP members,
Yes, I know that one cannot use GUI things from non-GUI threads. thats why i am trying to "do process" in worker thread(Non-GUI) than coming back to GUI thread(main thread) to display Graph. but getting following error.
C#
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QCustomPlot(0x15b9a988), parent's thread is QThread(0x14547f90), current thread is QThread(0x15ba56c8)

whar i am doing in my code is as following..
from Main thread going to worker thread(NON-GUI). Worked thread is processing data and writing it into a txt FILE. after finishing the processing I am trying to kill the worker thread.
in AnalysisworkerThread.cpp
AnalysisWorkerThread::BusLoadAvg();
    GenSetPayloadAna gSetPayloadAnalysis;
    gSetPayloadAnalysis.PayloadAnalysisDisplay();  
    //emit AnalysisThreadFinished();
    emit OnFinishAnalysis();


SIGNAL OnFinishAnalysis() is connected to SLOT AfterFinishAnalysis()
C#
connect(this, SIGNAL(OnFinishAnalysis()),this,SLOT(AfterFinishAnalysis()));


C#
void AnalysisWorkerThread::AfterFinishAnalysis()
{

   mwobj.DisplayGraph();  //mwobj is object of MainThread
   //DisplayGraph is a function in MainWindow.cpp
}

in mainWindow.cpp DisplayGraph() simply trying to upload a Sample graph
C#
void MainWindow::DisplayGraph()
{
    qDebug()<<"Back to Main Thread";
    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
    }
    
    ui->customPlot->addGraph();
    ui->customPlot->graph(0)->setData(x, y);
    ui->customPlot->xAxis->setLabel("x");
    ui->customPlot->yAxis->setLabel("y");  
    ui->customPlot->xAxis->setRange(-1, 1);
    ui->customPlot->yAxis->setRange(0, 1);
    ui->customPlot->replot();

}




WOULD BE NICE IF SOMEONE CAN help me where i am doing wrong .....
Thanks

What I have tried:

I am trying as i added above when i try to Kill the thread as it finished than it is going to crash the Application
in Anlysisworkerthread.cpp
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
Posted
Updated 11-Feb-16 2:36am
v2

1 solution

You can use an event as already suggested in my answer to your question update/change QListWidget item value[^]:

In a header file included by MainWindow.cpp and AnalysisWorkerThread.cpp (value should be application wide unique):
# define DISPLAY_GRAPH_EV (QEvent::User + 1)


Handle custom event in MainWindow.cpp:
void MainWindow::customEvent(QEvent *event)
{
    if (static_cast<int>(event->type()) == DISPLAY_GRAPH_EV)
    {
        DisplayGraph();
    }
}


Post the event when the worker thread has finished (the signal might be no longer necessary so the event can be also posted instead of emitting the signal):
void AnalysisWorkerThread::AfterFinishAnalysis()
{
    QCoreApplication::postEvent(pMainWindow, new QEvent((QEvent::Type)DISPLAY_GRAPH_EV)); 
}
 
Share this answer
 
Comments
XamBEE 12-Feb-16 11:03am    
Thanks . would be nice if you elaborate first line.
"(QEvent::User + 1)".
Jochen Arndt 12-Feb-16 11:11am    
Qt uses an enum for internal events (QEvent::Type). The last (highest) defined enum is QEvent::User. This and all higher values are available for user defined events. If you use user defined enums, they should be unique for each place where used. Here I gave just an example by adding one (e.g. when QEvent::User is already used for another user defined event).
XamBEE 12-Feb-16 11:51am    
getting error ":: may not appear in macro parameter list"
# define DISPLAY_GRAPH_EV (QEvent::User + 1)
Jochen Arndt 13-Feb-16 3:36am    
???
Ensure that there is a space before the parenthesis.
Alternatively use
const int DISPLAY_GRAPH_EV = QEvent::User + 1;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900