Thursday, 16 June 2011

Qt: why didn't I know about this earlier? (or how bored am I?)

There's not much to do in Jeneponto. A couple of weeks ago my fellow volunteer Alanna and I found ourselves one Saturday night inventing the sport of "Rokok Topi", where two players throw empty cigarette packets into a hat. One point for each packet lodged wholly within the hat. Add use of a fan for tricky cross winds, if that's too easy.

It's too late for inclusion in the 2012 Olympics - maybe Rio 2016, but I can foresee problems with the tobacco sponsorship element. But if McDonalds can be a sponsor, why not a manufacturer of pungent Indonesian cigarettes?

In my other idle moments (of which there are many), I decided to write some software for viewing results from the SFV flood model, using the Qt development suite. 

It's surprisingly easy, as Qt has a wide range of inbuilt graphics and rendering routines. There's also Qt Creator IDE for developing, building and debugging the code, and the help system is actually helpful too. It's also truly cross platform (Linux, mobile, and Windows if you must). Excellent value for money, especially as it's free.

The code for drawing a mesh of triangles, with colour corresponding to depth, looks like this:

    for(e=0;e<mesh.Ne;e++){
        triangle.clear();

        triangle << QPointF (mesh.getnode(e,1).x,-mesh.getnode(e,1).y) <<
                    QPointF (mesh.getnode(e,2).x,-mesh.getnode(e,2).y) <<
                    QPointF (mesh.getnode(e,3).x,-mesh.getnode(e,3).y);

        mainScene->addPolygon(triangle,pen,brush);
    }


And that's it. Add some code for loading data, changing colour scales and loading background raster and you get a fairly functional application. All told this has taken about a week - including trying to get back into C++ and learning new stuff from the Qt libraries.



It's far from perfect, rendering of large background images is unacceptably slow for example. There is also some frustration when it becomes very difficult to do something apparently simple - I haven't managed to work out how to change the colour of one button without changing the colour of all buttons generated afterwards.

But there's also support for databases (MySQL, SQLite, Oracle, ODBC), with a thing called model view, allowing you to link a table directly to a database, without much fuss. I wrote a simple SQLite front end as part of a prototype system for gathering social and poverty data in the field - took a couple of days. 

You can download Qt here - highly recommended. 

No comments:

Post a Comment