开发者

Qt Getting Response from SoapRequest

Consider the following in Qt using QtSoap lib:

 QtSoapHttpTransport http;
 http.setHost("XXXX",3333);
 connect(&http, SIGNAL(responseReady()), this, SLOT(getResponse()));

now there is a method i want to call which is:

QtSoapMessage request;
request.setMethod("test");
request.addMethodArgument("xxx","zzzz",xxx);
request.addMethodArgument("xx","xx",xx);
http.submitRequest(Request, "/api/soap");

now i want to have something like this :

QString GetTest(){
while(http.isBusy);   // no such a thing as isbusy 
return http.getResponse().returnValue().toString();}

or any technique i can use to get the return value or wait for it and g开发者_StackOverflow中文版et it..

Thanks in advance...


I don't see a problem. The QtSoapHttpTransport reference already has a nice simple example.

If you want to have a getter that blocks and returns only when the response is received, doing active wait (your while loop) is absolutely not a way to go.

You already connect the responseReady signal to your slot, so the only missing thing would be to have a synchronization point that blocks your thread calling getTest until this slot is executed.

class Messenger : public QObject {
    Q_OBJECT
public:
    Messenger() { /* ... your initialization code with connect ... */ }

    void sendRequest() { /* ... your sending code ... */ }

    QString getTest()  // call this from a worker thread to wait
    {                  // for a response to arrive and retrieve it
        QMutexLocker lock(&responseMutex);
        responseReady.wait(&responseMutex);
        return http.getResponse().returnValue().toString();
    }

public slots:
    void getResponse() {  // slot called by Qt event loop when response arrives
        responseReady.wakeAll();
    }

private:
    QtSoapHttpTransport http;
    QWaitCondition responseReady;
    QMutex responseMutex;
};

Note that this design only makes sense if you have a multithreaded application and the thread calling getTest is a working thread, not event-driven thread.

On the other hand, if your application just wants to do something with the received response, there is imho no reason why you need a blocking method in the first place. Just perform your actions in the slot directly - just like it is in the Qt documentation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜