XBMC-Remote-code-example-using-Qt-C

From Qt Wiki
Revision as of 18:12, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Creating an XBMC remote using Qt C++

I wanted to create an XBMC remote, but I couldn’t find any tutorials of how to do this. So after numerous hours of Googling and reading forums, I decided to write my own… This is written for the JSON Http Api Frodo. Found Here [wiki.xbmc.org]

This is a simple tutorial that will set you up with the basics of sending the Pause/Play command to XBMC over Http..

Lets get started…

To start I created a standard Qt Gui Application. Then I added libraries.. XBMC now uses JSON for communicating with its Http interface. Unfortunately Qt doesn’t come with a JSON parsing library..

However one has been built by Flavio Castelli: Found here [qjson.sourceforge.net]

we need to add it to our project so its included in the build. On Ubuntu I downloaded the qjson dev files in Synaptic Package Manager and just added these lines to the .pro file:

Windows might be slightly different… Stackoverflow has a few posts on it.

We then also need to add networking to the project as well.. So we add this line to our .pro file:

Setting up the Connection

first we need to set up the variables needed for the connection. For this we need a QNetworkRequest, QNetworkAccessManager and strings for username, password, hostname/ip and port. So add these to your header file.

then we need to set them up.. so we shall make a method that we can call from the constructor to set up the access manager and network request..
this method will set he request URL and header. It also links the network manager to a Authenticator Slot that provides the username and password. I only set the access details here for convenience, it would be better to let the user set them by a line edit.

These will provide the connection to XBMC.. So we can now call the access manager to post requests to XBMC.

Parsing the reply

Now we will set up a method to parse the reply. This is where we use QJson. It will parse the reply and return a QVariantMap that can be used to access info from the reply

Post Play/Pause request

Now the fun part.. We will post a play pause request to XBMC. This is done in 2 stages.. the first is to build the request and send it.. the second part is to accept and parse the reply.

To post the request we create a slot that can be connected to Ui items. (In this case a play/pause button). 'note': I use the ‘playerid’ of 1, but on more complex remote it would be prudent to get the if from XBMC.

The request JSON without the formatting to make it a valid QString itself looks like:

Now we have the code to post the request, we need a slot to deal with the reply. This slot will disconnect the request manager from itself. Then parse the reply data and use it accordingly

and thats our pause play request done!

You can now build on this to make a full XBMC controller. A handy hint to find out what needs sent and what is replied is to get Firefox and get the addon “Live HTTP Headers”. Then use XBMCs web interface and use live http headers to see the requests it sends and what it gets in response..

files in full:

header file:

cpp file: