Using a Simple Web Service with Qt
Материал из Wiki.crossplatform.ru
Qt Quarterly | Выпуск 23 | Документация |
Qt's networking classes allow all sorts of complex networked applicationsto be developed, but some Web services are still simple enough to be usedwith just a few lines of code and a bit of imagination.
Содержание |
One such service is provided by the MathTran project at www.mathtran.org.This open source project aims to provide services to automatically translatemathematical content into Web-friendly formats, such as PNG images and MathMLmarkup.Run by the Open University in the United Kingdom and funded by the JointInformation Systems Committee (JISC), the project currently provides a Webservice to translate into images.
In this article, we show how we can use Qt's networking classes in the simplestpossible way in a GUI application, using the MathTran Web service to preview markup entered in a text editor as an image.In the future, the MathTran project hopes to offer a Web service to translatebetween and presentation MathML which we could render using the MML Widget Qt Solution.
[править] Using the Service
At the heart of the MathTran service is a Web server.Clients send markup to the server byencoding it as part of the request URI of an HTTP GET request. The serverdecodes the input, passes it to a daemon, converts the output to an image, and sends back the appropriateHTTP response.
For example, the equation
is written like this:
\vec{\nabla}\cdot\vec{B} = 0
This text is encoded and appended as a query to the rest of the request URI,giving us the following string:
http://www.mathtran.org/cgi-bin/mathtran?D=3;tex=\vec{\nabla}\cdot\vec{B}%20%3D%200
All an application needs to do to obtain the image of an equation is to sendit to the service and wait for the response. Our application uses QUrl and QHttp to make this process as simple as possible.
[править] Setting Things Up
In the constructor of our Window class, we construct a QHttp objectto use to communicate with the server in a two-stage process:
- We connect the clicked() signal from the Update button tothe fetchImage() slot. This initiates the request to the server.
- Since we are interested in the result of a HTTP GET request, we connectits done(bool) signal to the updateForm() slot.
Omitting the details of setting up the user interface, the constructorcontains code to set up these connections:
Window::Window(QWidget *parent) : QWidget(parent) { http = new QHttp(this); connect(http, SIGNAL(done(bool)), this, SLOT(updateForm(bool))); ... connect(updateButton, SIGNAL(clicked()), this, SLOT(fetchImage())); ... }
[править] Sending and Receiving
Since we perform the request and receive the response using thesame QHttp object, we must take care to ensure thatthis two-stage process runs smoothly. In the fetchImage() function,we prevent the user from making another request while one is already inprogress by disabling the Update button:
void Window::fetchImage() { updateButton->setEnabled(false); QUrl url; url.setPath("/cgi-bin/mathtran"); url.setQueryDelimiters('=', ';'); url.addQueryItem("D", "3"); url.addQueryItem("tex", QUrl::toPercentEncoding( equationEditor->toPlainText())); http->setHost("mathtran.org"); http->get(url.toString()); }
We use the flexibility of the QUrl class to set up part of the requestURI. Since we want to let the QHttp object buffer the data obtainedfrom the server, we simply pass the string representation of the URL tothe object's get() function.
When this slot returns, the Qt event loop continues to process events, andthe QHttp object gets on with its job of managing HTTP requests andresponses. When the final response arrives, it emits the done(bool)signal and our updateForm() slot is called:
void Window::updateForm(bool error) { if (!error) { QImage image; if (image.loadFromData(http->readAll())) { QPixmap pixmap = QPixmap::fromImage(image); outputLabel->setPixmap(pixmap); } } updateButton->setEnabled(true); }
If no error occurred, we can convert the data we received to a pixmap foruse in a label. No matter if the request succeeded or failed, we alwaysre-enable the Update button to allow the user to try again.
[править] Exploring Further
The MathTran service, while designed to be simple, also exposes otherparameters that we can experiment with. The server software isavailable from SourceForge, so you can also run your own serverif you like.See the service's terms and conditions for more information about using the public MathTran server.
The source code for the example shown inthis article is also available.