08 October 2010

Web Services with Qt

To create a Qt client application that connects to a SOAP/WSDL web service, you should use the C++ library/tools from gSOAP. There have been Qt specific SOAP libraries, but they either dissapeared, or are feature incomplete. This is a simple how-to, explaining project setup for Qt development, assuming Qt Creator.

Create gSOAP code

To import all WSDLs to one header, ensure the server is running (irrelevant of source, web or file)

wsdl2h -o name.h http://service1?WSDL C:\service1downloaded.wsdl

To change the namespace prefix (used in soap variables), use the -n 'prefix' option. Then, to produce client-side working code and bindings:

soapcpp2 -C -i -I"C:\path\to\gsoap\imports" -x name.h

• -C : client-side code only

• -i : generate proxies

• -I : import path, necessary

• -x : without the example SOAP messages

Setting up your Qt Project

Add all generated code into your Qt project.
Open your .pro file and add:

INCLUDEPATH += path\to\gsoap

#ifdef Q_WS_WIN
LIBS += C:\Qt\VERSION\mingw\lib\libws2_32.a


Include the proxy class. Create a proxy object:
ServiceProxy proxy = new ServiceProxy();

Make request and response objects:

ns1__Function *request = new _ns1__Function();

ns1__FunctionResponse *response = new _ns1__FunctionResponse();

And then set the arguments:

request ->arg0 = &variable;
request ->arg1 = &variable;

Perform the communication, receiving a soap code, we then handle and perform tasks based on the response

int soap_code = proxy ->Function(request ,response)

if (soap_code == SOAP_OK)
    variable = response->OutVariable;
    variable = response->return

Special Quirks

I've found that the proxy class does not like to be included in headers, so instead we forward declare them if necessary and include it in the .cpp as per common practice when programming in Qt.

The *.nsmap and stdsoap.cpp must be included only once in the entire project.


Abdul said...

I am trying to make gSoap work with Qt. So far with no success. During my research I came across your article as well. I was wondering if you could help me. I have downloaded gSoap. My question is that it has a set of installation instructions itself which have my head spinning.

1. Do I need to follow them?

2. If YES, could you explain how?


Sam said...

I don't remember understanding their instructions well either.

I think i just unzipped it in the root of the drive and added the folder to my PATH so i could easily use the tools from command line.