2015-07-11 130 views
0

我有一个类'A',在这个类中有一个方法'method1()'和method1在qthread中运行。在method1中,我想调用MainWindow类中的函数,如下所示:'MainWindow window; window.func()”Qt线程调用函数

当我这样做,我收到此错误信息:QObject的::的setParent:无法设置父,新的父是在不同的线程

这里是我的代码:

void gCode_int::parse_File(char* gCode_file, int file_length,MainWindow *window) 
{ 
    window->fahre_on_pos(NULL,atoi(x_pos.c_str()),atoi(y_pos.c_str())); 
} 

在这里,在主窗口的功能:

void MainWindow::fahre_on_pos(char* g_command ,int x_pos, int y_pos) 
{  
double y_schritte; 
double x_schritte; 

int j = 1; 
int x_length = x_pos-x_pos_old; 
int y_length = y_pos-y_pos_old; 

digitalWrite(x_treiber,1); 
digitalWrite(y_treiber,1); 

if(x_length > 0) 
{ 
    digitalWrite(x_richtung, 1); 
} 
if(x_length <= 0) 
{ 
    digitalWrite(x_richtung,0); 
    x_length *= -1; 
} 

if(y_length > 0) 
{ 
    digitalWrite(y_richtung, 0); 
} 
if(y_length < 0) 
{ 
    digitalWrite(y_richtung,1); 
    y_length *= -1; 
} 
y_schritte = round((y_length/1.25)*48); 

if(y_schritte == 0) 
{ 
    y_schritte =1; 
    digitalWrite(y_treiber,0); 
} 
if(x_schritte == 0) 
    digitalWrite(x_treiber,0); 

x_schritte = round(((x_length/1.25)*200)/y_schritte); 
while(j <= y_schritte) 
{ 
    for(int i=1;i<= x_schritte;i++) 
    { 
     digitalWrite(x_v, 1); 
     delay(1); 
     digitalWrite(x_v, 0); 
     delay(1); 
    } 
    if(x_schritte == 0) 
    { 
     digitalWrite(y_v, 1); 
     delay(4); 
     digitalWrite(y_v, 0); 
     delay(4); 
    } 
    else 
    { 
     digitalWrite(y_v, 1); 
     delay(1); 
     digitalWrite(y_v, 0); 
     delay(1); 
    } 
    j++; 
} 
x_pos_old = x_pos; 
y_pos_old = y_pos; 

} 

希望有人能帮助我

回答

0

http://doc.qt.io/qt-5/threads-qobject.html

虽然QObject的是可重入的,所述GUI类,特别是QWidget的和所有它的子类,不可重入。他们只能从主线程使用。如前所述,QCoreApplication :: exec()也必须从该线程中调用。

实际上,在主线程之外的其他线程中使用GUI类的不可能性可以通过在独立的工作线程中放置耗时的操作并在工作线程中在主线程的屏幕上显示结果完成

信号和槽应该让工人和主窗口之间的连接器,否则说明它不会工作