-
Notifications
You must be signed in to change notification settings - Fork 0
Streaming Data
In this example I'll show you how you can build a little web-service with QtGui and Nurias HTTP classes which lets HttpClient do the job of transferring data.
You should've read the Hello Browser and File Delivery examples before trying this.
The webservice we develop in this example will use what we know from the previous two to serve the user a simple HTML form. When filled out and submitted, he'll be able to view an image containing his input rendered on the server. I'll use this opportunity to introduce you to HttpClient's pipeToClient() method :)
As in the previous examples, create your project file. For this example, we also need the QtGui module, so include it too: QT += gui. Mine looks like this
This time the main.cpp is a bit more complex. First, you'll want to #include the headers QUrlQuery to parse the request URL later on, QPainter and QImage to draw something and QBuffer as buffer device.
Next, make sure you're using QGuiApplication for this one, else your application will crash when drawing!
Let's dive into the interesting part: The actual code. So, we first set up a server again and let it serve static files (reference 1). We do this so we can serve the HTML page without having to write it in C++.
Note: Like the special index slot in HttpNode, when serving files index.html will be served if there's no slot for index and static file support is enabled.
I've included a simplistic index.html file which draws some HTML formular we can use as user front-end. Not pretty, but does the job ;)
Back to C++. Next we implement the method which will do the actual rendering, which I've called renderSlot. Like in Hello Browser, this method takes a Nuria::HttpClient* as argument. Then I ask the client instance the requested path and use QUrlQuery to get the "text" GET parameter (reference 2). That's all we need to know, so we create a QImage and make sure it's all white (reference 3). Next, we instantiate a QPainter and draw the text onto it (reference 4).
Note: This example is supposed to show how HttpClient can be used, so sorry to anyone who thinks I'm rushing the QImage stuff too much.
Now, we create a QBuffer and tell the QImage to store itself as PNG data into that buffer (reference 5). We do that because it's really convenient to work with QIODevices with HttpClient. Instead of having to deal with sending the data now, we can offload that work over to HttpClient. This is especially great because the data could be a really big file - In which case loading the data completely into memory would be a bad idea. HttpClient on the other hand won't do that, but instead will send the data in chunks to the client.
Sounds great? Well, then just reset() the QBuffer because HttpClient will only read onward from the current position in the buffer - But we just wrote something into it, so its current position is behind the image data. And last but not least, call pipeToClient() on the client passing the buffer. (reference 6)
HttpClient will take ownership of the buffer, thus no memory is leaked and we don't have to write additional code.
Back to the main(). We still need to connect our new renderSlot method. We do that using connectSlot(), I called it "render" (reference 7).
Afterwards, only thing missing is starting the server again like in the previous examples (reference 8).
And that's it, you can now go to http://127.0.0.1:8080/ with your web browser and render a image with some text on it through the intuitive web interface provided in index.html :)
Next up, we will extend this example so the user can supply a custom background image!