Unsolved slot/signal in QtConcurrent
-
@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.
-
@KroMignon
in my case who is the signal and the slot please ? -
@lindaah said in slot/signal in QtConcurrent:
in my case who is the signal and the slot please ?
Strange question!
Do you have understand how signal/slot mechanism works on Qt?
==> https://doc.qt.io/qt-5/signalsandslots.htmlsignals
andslots
are part of aQObject
based class.
They are used to enable communication betweenQObject
based classes. -
@KroMignon i just started working with c ++ and qt
-
@KroMignon it's complicated for me can you please explain me a little more please
-
@lindaah
Did you take the time to read the signals/slots chapter people are referring you to? If I were you and new to C++ and Qt, I would write a couple of tiny programs with no threads to get familiar with signals and other Qt/C++ concepts, before returning to your current code with better understanding. -
@lindaah said in slot/signal in QtConcurrent:
it's complicated for me can you please explain me a little more please
It will stay complicated to you as long as you don't take time to learn what C++ and Qt is.
Sorry, but I can not learn for you, this is something you have to do yourself.
I can try to give you some hints or help you to understand but doing the job for you is not an option for me.To create programs, you have to invest time and learn.
I would suggest you to start why reading Qt documentation and try to build/start/modify Qt demo application.
So you will becomes comfortable with C++/Qt basic concepts and then start with more complicated stuff like multi-threading. -
@JonB I know from the start that I have to work with signal and slot and I read the forum but it's complicated for me that's why I asked this question.
I have to work with thread :( -
@lindaah Did you read the link which was given to you two times already?
Signals/Slots is one of the core concepts in Qt. So, please take time to learn it...