Unsolved slot/signal in QtConcurrent
-
@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
-
Only from your gui thread. You can send a signal to your gui therad and change it there.
-
@Pl45m4 how ? can you give me an exemple please?
@KroMignon
but why did they walk for 13 times then please ?
in fact after the 13th time I had this error -
@lindaah said in slot/signal in QtConcurrent:
but why did they walk for 13 times then please ?
Because you was 13 times lucky...
-
@KroMignon how can i correct it please
-
@lindaah said in slot/signal in QtConcurrent:
how can i correct it please
It is simple, do use QObject based items in their thread ;-)
How exactly depends on what you are doing!
There is no magic method. -
@KroMignon but I would need in a thread to activate another button
that's why I did
this->ui->Cameratoggle -
@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 ok thanks :)
-
@KroMignon I can't do it can you please give me an example
-
@lindaah said in slot/signal in QtConcurrent:
I can't do it can you please give me an example
What did you try?
Show me the code you are trying and I can give you advice. -
@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.
-
@SGaist yes I know but how can I correct it?
@KroMignon can you help me please -
@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...
-
@jsulm but i haven't found how i can work with it
can you help me please -
@lindaah Do you know how to use signals/slots in Qt?
If not please first read https://doc.qt.io/qt-5/signalsandslots.html -
@lindaah said in slot/signal in QtConcurrent:
can you help me please
You have all information you need, as @SGaist, @jsulm and I told you: All UI component have to be updated in main thread.
Multi-threading is always complicated, so first you have to ask yourself if you really need an extra thread.If it is the case, then you have to do it in the right way:
- do all "heavy/long" computation in the dedicated thread.
- transfer the result to the main thread
- update you UI in the main thread.
How you sent those values to main thread is up to you:
QtConcurrent::run()
combined withQFutureWatcher
- emitting signals
- use
QMetaObject::invokeMethod()
- etc.
This is up to you.