Older blog entries for e8johan (starting at number 361)

GTK+ made Qt

Disclaimer! First of all - this is not an attack on any toolkit, nor critique. Rather, it is a hacker sitting down and trying out an idea.

It has been a couple of years since I first tried out this idea (some seven, I believe). This time, licensing permits it and I believe that the timing is right. So, without further ado, let me introduce GTK+ made Qt.

Qt and GTK+, and all other toolkits out there, provide more or less the same functionality (take that with a grain of salt, but the core is true). So, my idea was to wrap Qt in a GTK+ API layer. Just to see if it could be done. Attacking the problem at this level might be considered stupid - but I like to get results fairly quickly, and this way I can work with GTK+ code directly, instead of writing a ton of underlaying code just to get something to compile.

There are tons and tons of corner cases and not so corner-ish cases that can make this break. The implementation is miles and miles from perfect and the code I've been testing it on is rather trivial. What I've done is that I've taken one of the examples of the GTK+ 2.0 Tutorial and made it do what it is supposed to do (sort of). Examples of what I've ignored in the process:
  • Event handling functions get NULL instead of a GdkEvent pointer.
  • Packing flags (e.g. homogenus and spacing when calling gtk_hbox_new) ignored - I simply use QVBoxLayout, QHBoxLayout and QGridLayout where they fit best.
  • Only a minimalist part of the API has been implemented (the process is build, add missing symbols, repeat)
  • Most GtkXxx classes are defined to a QWidget. Objects are casted up and down all over the place to work around this.
However, the problem isn't really matching the APIs to each other and getting every pixel right. At least, that is not the current problem. Instead, matching Qt's C++ style OO to GTK+'s is the problem. What are the differences? I hear you ask.

To create a trivial dialog in Qt, you inherit the QDialog class and then create the children in the constructor of your new class. In GTK+, you generally create a function for creating the dialog in question. In that function, all widgets (including the dialog) are created and put in a proper hierarchy. The resulting hierarchy of widgets is (95%) identical, and all the custom code to make it tick is there - it is just placed differently in the source.

Another difference that has a large impact is the fact that Qt event handles are virtual methods of QObjects and slots are QObject methods. In GTK+, they are plain C functions that are passed as function pointer to g_signal_connect calls.

So, the starting point is this example. I've placed that code in helloworld.cpp (we need it to build as a C++ source file, I'm sure that GCC can be convinced of that by other means, e.g. by using g++ directly, but just changing the extension does it for me).

The only change made to the source code itself is that the include of gtk/gtk.h has been replaced by the inclusion of gtk-made-qt.h (no, the name is not critique, it is just my strange humor). The header file is the container of the rest of this experiment.

The GTK+ Hello World example running in QtCreator on Windows Vista

First of all, I've made some rather rude typedefs and defines with regard to GLib (this just shows how much that needs to be redone to do this properly):

#define G_OBJECT(obj) ((QObject*)(obj))
#define G_CALLBACK(func) ((void*)(func))

typedef void* gpointer;
typedef bool gboolean;
typedef char gchar;

#define g_print qDebug


There is more of this of course, and some of it is spread out across multiple header files. Next follows the same horrific crime, but this time made to some of the GTK+ widgets:

#define GtkWidget QWidget

void gtk_widget_show(QWidget *w) { w->show(); }

QWidget *gtk_window_new(int) { return new QWidget(); }
void gtk_window_set_title(GtkWindow *w, const char *t) { w->setWindowTitle(QString(t)); }


#define GTK_BOX(obj) (obj->layout())
void gtk_box_pack_start(QLayout *l, QWidget *w, bool expand, bool fill, int padding) { l->addWidget(w); }

As you can see, straight forward, brute force, get the job done hacks. All this shows how close the different APIs match. This is trivial code (albeit unsafe) and adds almost no extra conversions or checks.

The really interesting part is the implementation of the g_signal_connect function. Here, bridges for both events and signals/slots are dynamically setup. So, here it is in all its glory:

void g_signal_connect(QObject *src, const char *cstrEventName, void *f, void *data)
QString eventName = QString(cstrEventName);

if (eventName.endsWith("_event"))
{ // This is an event, f is an eventFuncPtr
QObject *o = QGtkEventFilter::createFilter(eventName, eventFuncPtr(f), data);
qWarning("Failed to match GTK event '%s' to a Qt event filter.", cstrEventName);
{ // This is a callback, f is an callbackFuncPtr
QObject *o = new QGtkCallbackBridge(src, callbackFuncPtr(f), data);

const char *signalName = 0;
if(eventName == "clicked")
signalName = SIGNAL(clicked());

if (signalName)
QObject::connect(src, signalName, o, SLOT(trigger()));
qWarning("Failed to match GTK signal '%s' to a Qt signal.", cstrEventName);

To my surprise, that is all that it takes. The QGtkCallbackBridge is a trivial QObject that calls the given function pointer with the QObject::sender() and the given data pointer when signalled. The QGtkEventFilter is not much more complicated. It simply filters out the event in question and triggers the given function pointer in much the same way. I've created a factory for the event filters, as I want them to re-create something looking like the original GdkEvent structure. There is actually one event filter class for each type of event, just so that this code can be added.

Well, I've not packaged the code yet. My goal is to try at least one more example first (perhaps two, given time). In the mean time, let's see if this can be made useful (a large enough portion of the GTK+ API needs to be implemented for that) - and if the idea is interesting at all (from a political standpoint, etc). As it stands now, this might help porting GTK+ applications to Qt only platforms, or it could be used as a migration kit (not that I encourage people to migrate from GTK+ to Qt - it is a choice that every developer has to make - use whatever feels comfortable to you).

Syndicated 2010-01-15 15:47:00 from Life of a Developer

The wether forcast mentioned snow on Wednesday. We had some yesterday and today it is really snowing away. Looks like I'll have to buy a sledge for Lisa! Will be great fun.

Right now I'm sitting, writing about Qt's new animation classes released in 4.6. Looking at the snow, writing about this, makes me wonder if it wouldn't be a good idea to implement animations that delete themselves and their items when the item leaves a given bounding rectangle.

Why? Then you can define a bounding rectangle, e.g. the screen, and create snow flakes with randomized animations and just let them drop.

I remember doing something like that back in the STOS/AMOS days. You created a sprite, defined an animation pattern and then forgot about it (as I recall). This was great for the 2D shooters of the time with aliens attacking in predefined patterns. Not 100% sure how the surviving aliens (yes, you have to take bad players into account, including me) where handled. The ones you hit could be taken care of in a collision detection handing function called from the inner loop... well, nostalgia might make me look at this in a forgiving light.

Anyway, the snow is falling. There will be more tomorrow. It will probably be cold all the way to Christmas. Looks like we're in for a white Christmas.

Syndicated 2009-12-15 09:27:00 from Life of a Developer

One month of flying

It has been a month since I started working in my own business. My former employer has been very kind and let me explore this while on a leave of absense, i.e. I have somewhere to return to.

This Monday I sent my first large invoice to my biggest customer. Now, time to wait for the next pay. What have I learned this far?
  • Bookkeeping - there are a lot of receipts to handle, especially when starting. Insurances, web servers, car insurance, etc.
  • Working from home - it works fairly well for me. I've come up with a scheme to keep myself motivated and actually working. However, a simple business lunch now takes three hours instead of one.
  • Money takes time - I've been given offers, signing deals, working more than ever, and my next pay check is still not due until mid January. Worth thinking about.
  • Time is not money - at least not yet. By being self employed I'm so much more flexible so that I get more quality time with my family, while making the money that I need (and some extra, but I guess that is for future expenses).
So, one month done, many more to come. This far, I'm looking forward to work every day.

Syndicated 2009-12-02 11:52:00 from Life of a Developer


So, it is kind of stupid to write about these things afterwards, but the time before a conference is always filled with preparations and far too much time spent in OO.o Impress.

The last Saturday I attended FSCONS'09. This turned out to be another great event (I've been there two years now). I met lots of interesting people and got faces to a couple of names who's blogs I've been following for a while now.

My plan for this year was to do two workshops - one on Qt for Embedded Linux (which I try to focus on), and one on the Qt SDK, trying to get people started. Both these sessions turned into something else - probably due to the unclearness of what a workshop is intended to be.

The Embedding Qt workshop (slides) had a great turn-up. The idea was to go through the configuration process of Qt/X11, Qt/embedded targetted at QVFb and then have a look at the configuration and deployment of Qt for an actual embedded target. Given a 90 minutes slot, I knew that compilation time was going to be an issue. When it turned out that only one attendee wanted to give it a try, this session turned into an ordinary talk (the one brave guy gave up after the Qt/X11 build - thank you still!). However, the talk was great, lots of interesting questions and great feedback. I really enjoyed the interaction with the audience and at the end Knut Yrvin (an actual norwegian troll) showed up, adding even more facts to the discussion.

The Qt SDK workshop (too few slides to share) also had a great turn-up. My plan was to get people to install the SDK and then show them the basics. I think that something like 5-10 people actually ran the installer, so that was a great success. The demo, however, could have needed some more planning from my side. I showed signals/slots, some widgets and the concept of layouts. For that, I used 40 minutes of my 30 minutes slot, so there where loads of unaswered questions at the end (thanks for everybody coming up to me afterwards having a chat).

To summarize - my aim with workshops did not really work in this setting, however, the quality of the audience and the resulting discussions turned this into a great event for me. I hope that you liked it as well!

Syndicated 2009-11-15 14:27:00 from Life of a Developer

Warning - broken links ahead

So, the day finally came when I switched from a fairly broken and badly styled MediaWiki-based web page to a WordPress-based one instead. This not only means better management abilites, a working search engine, a proper editor, etc. It also means that links are broken. I don't write are likely to be - there are broken links out there as I have not migrated all contents. If you run into one - feel free to tell me and I'll try to sort it out.

The result from this move will hopefully be a more professional presence on the web for my part. Unfortunately I do not have the time I want to polish the site - now I have to prepare my workshops (1, 2) for FSCONS. These events always seems to be so far away into the distant future when I sign up for them - and now it is only two weeks left...

Syndicated 2009-11-01 13:29:00 from Life of a Developer

Off to DevDays

I'm packing and trying not to forget anything, because my brain will be in auto-pilot-mode tomorrow at 4:50 when the taxi comes around to start my journey to DevDays'09 in Munich.

I'm really looking forward to this, and this year, I'll even get to speak a few words in the Qt in Education track on Monday. But the biggest upside is to meet all the people in real life. I'm especially looking forward to meeting the QtCentre crew - and all the trolls of course.

Syndicated 2009-10-11 07:26:00 from Life of a Developer


I've finally made the switch on one of my working, production mode, computers. The switch from KDE 3.x to 4.2.2 (I think - whatever Kubuntu comes with). I must admit having been sceptical to all the hype about plasma here, plasma there. Social desktop? I want my old workstation look and feel - I thought.

I absolutely love KDE 4. There is so much new, but everything that is different feels intuit. My very favorite feature is, this far, the device notifying plasmoid. Not having to show the desktop to mount, open or unmount my USB-stick is just - smart and intuit.

Now all I have to do is to find out how to change the clock to a 24h mode... (just noticed, taking the screenshot, after using the desktop for a week...)

Syndicated 2009-10-05 14:15:00 from Life of a Developer

Speaking - live!

To all of you in the Stockholm region, I will speak about Qt at ECS'09, the GUI session. Go there, it will be fun. I've even got source code in my slides :-)

Syndicated 2009-10-02 10:49:00 from Life of a Developer

QtCreator Plug-ins

I just saw that Prashanth Udupa has published a guide to writing QtCreator plug-ins - nice!

Syndicated 2009-08-31 13:48:00 from Life of a Developer

352 older entries...

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!