Effective Qt in ruby (part 3)

This is the third article in my series on writing Qt applications in ruby. I was planning to write about the declarative GUI system that I use in kaya, but a comment on one of my previous posts motivated me to take a small detour, and illustrate a very simple technique to extend a qtruby application with C++ code.

So, suppose you need to expose a C++ function like:

void applyEffect(QImage* img, float arg);

that takes a QImage, an argument, and applies a graphic effect, mutating the image in place.

Directly exposing this function to ruby using the extension API is not easy, because you need to extract a QImage pointer from the ruby object corresponding to the QImage, and that would require you to make assumptions on exactly how QObjects are wrapped by the ruby binding code, which is not ideal for a number of reasons.

Fortunately, there exists an elegant solution to this problem. First, you need to define your C++ function as a slot of some QObject. For example:

class Extensions : public QObject
public slots:
  void applyEffect(QImage* img, float arg) const;

Then in your extension initialization function you can instantiate it with something like:

Extensions* ext = new Extensions(QCoreApplication::instance());

And finally access it from ruby code and wrap it in a nicer package:

$ext = $qApp.findChild(Qt::Object, "__extensions__")
class Qt::Image
  def apply_effect(arg)
    $ext.applyEffect(self, arg)

This works because Qt allows you to call slots dynamically using runtime introspection of QObjects. It’s not as fast as a native function call, but in the context of a ruby method call, the additional cost should be pretty much negligible.

Of course, unless your extension is particularly large and complicated, you don’t need to create an Extension object for each of the functions you want to expose: you can add all of them as slots in a single Extension object, which is loaded at startup, and create a ruby-esque API for them directly in ruby code.


3 Responses to “Effective Qt in ruby (part 3)”

  1. knight Says:

    wow I didn’t know kaya… awesome project!!!

  2. Steve Conover Says:

    What platform are you developing on? I’ve been trying to get some form of qt plus ruby working on Snow Leopard over the past few months and it seems completely broken.

    • pcapriotti Says:

      I work on Linux. I tried installing qtruby on Windows at some point, it was quite painful (mainly because I’m not really familiar with Windows), but I managed to run kaya successfully in the end.
      I’ve never tried it on Mac OS X, but I don’t see why it shouldn’t work there. Try asking on #qtruby on freenode for more information.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: