2010-10-24 84 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文檔中一樣。