slot/signal in QtConcurrent
-
hi I have 2threads that I would like to activate them at the same time but after some try I had this problem :
Object: Cannot create children for a parent that is in a different thread. (Parent is QTextDocument(0xaa695e78), parent's thread is QThread(0xba7c18), current thread is QThread(0xbbde50) 2021-07-07 10:32:49 sigHandler: Unhandled signal 11, terminating
code :
void Lid::on_Lid_clicked() { QFuture<void> fut1 = QtConcurrent::run(IMU, this->Folder, this->ui->label_IMU, this->ui->IMU ); QFuture<void> fut = QtConcurrent::run(graphique, this->folder ,this->ui->IMU ,this->ui->Cameratoggle); }
after a search on the internet I found that it is necessary to use the signal and slot I tested this code :
void Lid::on_Lid_clicked() { QFutureWatcher<void>Watcher1; Watcher1.setFuture(QtConcurrent::run(IMU, this->Folder, this->ui->label_IMU, this->ui->IMU ); QFutureWatcher<void>Watcher; Watcher.setFuture(QtConcurrent::run(graphique, this->folder ,this->ui->IMU ,this->ui->Cameratoggle)); connect (&Watcher1,SIGNAL(reset()),&Watcher,SLOT(reset())); }
but still the same problem could you help me
-
@lindaah said in slot/signal in QtConcurrent:
Object: Cannot create children for a parent that is in a different thread.
(Parent is QTextDocument(0xaa695e78), parent's thread is QThread(0xba7c18), current thread is QThread(0xbbde50)This is the issue here. You cannot set a parent which lives in another thread.
What does your functions
graphique
andIMU
do? Is it neccessary for them to access ui elements? Because you shouldnt do that (ui->IMU
) -
@lindaah said in slot/signal in QtConcurrent:
so how can we access the ui element, because it s neccessary for me
You can only update ui element in main thread.
And you should only access them in main thread.cf. https://doc.qt.io/qt-5/thread-basics.html#gui-thread-and-worker-thread
-
@lindaah said in slot/signal in QtConcurrent:
but I would need in a thread to activate another button
Then you have to do it in button thread.
There are many way to do it, for example:
- emit a signal which is connected to this slot.
- use
QTimer::singleShot()
- use
QMetaObject::invokeMethod()
-
@KroMignon I have this qchart
and I would like to visualize it in a label and save it as a png
code :
void graphique(QString folder, QLabel *labelgraphe)
{
.
.
.
.
.
QChart *chart = new QChart(); // Pointer sur charte
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->axes(Qt::Vertical).first()->setRange(-3, 3);
chart->axes(Qt::Horizontal).first()->setRange(-10, 10);chart->setBackgroundBrush(QBrush(Qt::black)); QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); QMainWindow window; window.setCentralWidget(chartView); window.resize(500, 500); QString fileim = QDateTime::currentDateTime().toString(QString("'%1/graphe_'yyyy_MM_dd'_'hh_mm_ss'.png").arg(dossier)); QPixmap p = chartView->grab(); labelgraphe->setPixmap(p); p.save(fileim);
}
void Lid::on_Lid_clicked()
{
QFuture<void> fut1 = QtConcurrent::run(IMU, this->Folder);
QFuture<void> fut = QtConcurrent::run(graphique, this->folder ,this->ui->labelgraphe);
} -
Hi,
As already stated by my fellows, you can't manipulate GUI elements from other threads than the main thread and it's exactly what you do in your "graphique" function.
-
@lindaah said in slot/signal in QtConcurrent:
but how can I correct it?
You already was told to emit a signal in your thread. Then you connect that signal to a slot in GUI thread where you update your UI...