Reputation: 17
I have a Class 'A' and in this class there is a method 'method1()' and method1 run in a qthread. In method1 I'd like to call a function in the MainWindow class like this: 'MainWindow window; window.func()'
When I do this i get this error message: QObject::setParent: Cannot set parent, new parent is in a different thread
Here is my Code:
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()));
}
Here the function in MainWindow:
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;
}
Hope anybody can help me
Upvotes: 0
Views: 974
Reputation: 1427
From http://doc.qt.io/qt-5/threads-qobject.html
Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread. As noted earlier, QCoreApplication::exec() must also be called from that thread.
In practice, the impossibility of using GUI classes in other threads than the main thread can easily be worked around by putting time-consuming operations in a separate worker thread and displaying the results on screen in the main thread when the worker thread is finished
Signals and slots should make connection between workers and main window, otherwise as stated it will not work
Upvotes: 0
Reputation: 2050
Create a slot in MainWindow which receive parameters from thread with a signal and invoke the method
Qt - updating main window with second thread
Upvotes: 0