0

I was wodering if it matter where the connection is made on an object. For example, usally I do it before I make the call that might emit signals, but sometimes I have to do it afterwards if it's node on for example a QNetworkReply that doesnt exists until the call is made. Maybe I have to make some checks as well before the connection can be made.

Is there any chance that the signal can be emitted BEFORE a connection was made to connect this signal to a slot?

For example:

ClassA::function() {

   ClassB b;

   b.someCall();

   connect(&b, SIGNAL(finished()), this, SLOT(someSlot()));
}

ClassB::someCall() {

    emit finished();

}

Would the slot be called here? This might not be a very practical example since you would probabbly just use a return value in this case. But in some cases I do this, for example if "someCall" is a routine that you can step though and it can fail, either if a network-request inside of that routine fails, or if we get some error right in the beginning. Either way, the "failed()" signal should be emitted and have a slot to handle any kind of failure. For example;

ClassB::someCall() {

   allocate "something"

   if(something == NULL) emit finished();


   QNetworkReply *reply = someNetworkAccessManager.put(something);

   connect(reply, SIGNAL(finished()), this, SIGNAL(finished())); 

}

So here we have a call where it can emit finished() pretty much straight away, or after a while. If I create the connection after I make this call, will the first finished() be catched?

4

1 に答える 1

1

No, moc handles the connect macro's for QT, and creates all the underpinnings necessary to do the magic of signals and slots. as long as the visibility of where moc adds the code to the project is within scope of the compiler when its trying to resolve function calls, all will be well. And in practice, we put those connect calls as early on as possible, ie after an object is declared, and before it's used, in the header.

于 2012-05-11T01:05:02.050 に答える