2010-10-24 87 views
2
响应

考虑使用QtSoap LIB Qt中的以下内容:Qt的充分利用SoapRequest

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

现在是我想调用一个方法是:

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

现在我想有一些像这样的:

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

或任何技术,我可以用它来获得的返回值或等待并得到它..

在此先感谢...

回答

-1

我没有看到问题。 QtSoapHttpTransport reference已经有一个很好的简单例子。

如果你想有一个只有在接收到响应时才阻塞并返回的getter,那么执行主动等待(你的while循环)绝对不是一种方法。

您已经将responseReady信号连接到您的插槽,因此唯一缺少的将是有一个同步点阻止您的线程调用getTest,直到执行此插槽。

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

注意,这样的设计才有意义,如果你有一个多线程应用程序和线程调用getTest是工作线程,而不是事件驱动线程。另一方面,如果你的应用程序只是想做一些事情与收到的响应,没有理由你为什么需要一个阻塞方法的第一个地方。只需在插槽中直接执行你的操作 - 就像在Qt文档中一样。