从非gui工作线程错误调用gui线程 [英] Calling gui thread from non-gui worker thread error

查看:103
本文介绍了从非gui工作线程错误调用gui线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好CP成员,

是的,我知道不能从非GUI线程使用GUI的东西。这就是为什么我试图在工作线程(非GUI)中进行处理而不是回到GUI线程(主线程)来显示图形。但得到以下错误。

 QObject:无法为的父级创建子 -keyword>   另一个主题。 
(父 QCustomPlot(0x15b9a988),parent ' s线程是QThread(0x14547f90),当前线程是QThread(0x15ba56c8)



whar我在我的代码中做的如下..

从主线程转到工作线程(NON-GUI)。工作线程处理数据并将其写入txt文件。完成处理后我试图杀死工作线程。
$ AnalysisworkerThread.cpp中的b $ b

 AnalysisWorkerThread :: BusLoadAvg(); 
GenSetPayloadAna gSetPayloadAnalysis;
gSetPayloadAnalysis.PayloadAnalysisDisplay();
/ / emit AnalysisThreadFinished();
emit OnFinishAnalysis();





SIGNAL OnFinishAnalysis()连接到SLOT AfterFinishAnalysis()

 connect( this ,SIGNAL(OnFinishAnalysis()),,SLOT(AfterF inishAnalysis())); 





  void  AnalysisWorkerThread :: AfterFinishAnalysis()
{

mwobj.DisplayGraph(); // mwobj是MainThread的对象
/ / DisplayWraph是MainWindow.cpp中的一个函数
}



在mainWindow中。 cpp DisplayGraph()只是试图上传样本图

  void  MainWindow :: DisplayGraph()
{
qDebug()<< 返回主线;
QVector< double> x( 101 ),y( 101 ); // 初始化条目0..100
for int i = 0 ; i< 101; ++ i)
{
x [i] = i / 50. 0 - 1 ; // x从-1变为1
y [i] = x [i] * X [I]; // 让我们绘制二次函数
}

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();

}









将会是好,如果有人能帮助我,我做错了.....

谢谢



我尝试过的:



我正在尝试,因为我在上面添加时尝试杀死线程,因为它完成了它将导致应用程序崩溃
$ b $ a in anlysisworkerthread.cpp

connect(this,SIGNAL(finished()),this,SLOT(deleteLater()));

解决方案

< blockquote>您可以使用我在回答您的问题时建议的事件更新/更改QListWidget项目值 [ ^ ]:



在MainWindow.cpp和AnalysisWorkerThread.cpp包含的头文件中(值应该是应用程序范围唯一的):

 #d efine DISPLAY_GRAPH_EV(QEvent :: User + 1)





处理MainWindow.cpp中的自定义事件:

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





工作线程结束后发布事件(可能不再需要信号)所以事件也可以发布而不是发出信号):

 void AnalysisWorkerThread :: AfterFinishAnalysis()
{
QCoreApplication :: postEvent(pMainWindow) ,新的QEvent((QEvent :: Type)DISPLAY_GRAPH_EV));
}


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.

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()

connect(this, SIGNAL(OnFinishAnalysis()),this,SLOT(AfterFinishAnalysis()));



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

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()));

解决方案

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)); 
}


这篇关于从非gui工作线程错误调用gui线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆