Y
Y
Yu Yu2015-03-04 21:40:35
Qt
Yu Yu, 2015-03-04 21:40:35

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;
}

That is, you need to wait until k is received, otherwise there is nothing to return.
How to competently make the expectation of receiving a variable k?
PS !done it's me, for the situation, because I don't know what to do

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Armenian Radio, 2015-03-04
@gbg

Change the architecture so that the worker runs in a separate thread and sends a signal when ready.

A
AxisPod, 2015-03-05
@AxisPod

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.

D
Don Kaban, 2015-03-05
@donkaban

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.

A
Alexander Orlov, 2015-03-12
@alexis031182

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 question

Ask a Question

731 491 924 answers to any question