Answer the question
In order to leave comments, you need to log in
What can replace waiting loops in Qt?
Suppose there is a callable function that returns an integer:
int getNumber(void);
And there is some work in this function on the signal attached to the slotGET slot when an int is received.
int k;
int getNumber(void) {
emit signalGET();
/*--------------------------*/
while(!done){}
/*--------------------------*/
return k;
slotGET
{
k = /*что-то там*/;
emit done;
}
Answer the question
In order to leave comments, you need to log in
Change the architecture so that the worker runs in a separate thread and sends a signal when ready.
And then you will ask why is the computer slowing down? It is impossible to do such infinite loops, never. The core is completely 100% consumed.
add a signal and a POST slot to the GET slot, say. Why wait, you have asynchronous interaction, so work in an event-driven environment, since this is Qt.
If you have both signalGET and slotGET connected via DirectConnection, and there are no asynchronous operations on the slotGET slot, then no wait loops are needed. The execution context after calling signalGET will only go further in the method after the work inside signalGET is completed.
Otherwise, you can use different approaches. For example, with QEventLoop, the code might look like this:
int k;
int getNumber(void) {
emit signalGET();
QEventLoop event_loop;
connect(this, SIGNAL(done()), &event_loop, SLOT(quit()));
event_loop.exec();
return k;
}
slotGET
{
k = /*что-то там*/;
emit done;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question