Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Now I remember why I don't like C++

I have refac­tored IUP/Qt in­to a love­ly ob­ject hi­er­ar­chy. And I now re­mem­ber why I dis­like C++.

Here's the ba­sic prob­lem (code may be wrong, I don´t have the bro­ken ver­sions any­more):

I have a base class IOb­jec­t, that has stub set­ter­s/get­ters for all the IUP at­tributes.

For each kind of wid­get, I cre­ate a class that in­her­its from IOb­ject and the match­ing Qt wid­get (ex­am­ple: QDi­alog).

These have to be hooked in­to an Ihan­dle struc­ture through the use of a void * (re­mem­ber, that's C, I have that too ;-)

Now, this was my first naive ap­proach:

Ihandle *n;
IQDialog *d=new IQDialog();
n->handle=d; //handle is a void*

That com­piles ok. How­ev­er, when you try to call a d mem­ber lat­er (and I know I am us­ing old C cast­s... they had worked for me un­til to­day!) ...

((IObject *)(n->handle))set_title("Title here");

Blam­mo, seg­fault. Why? Be­cause in one of the as­sign­ments to/from void *, the point­er for some rea­son starts to point else­where.

Here´s what Glip­tic at #C++ sug­gest­ed, and it does work:

Ihandle *n;
IQDialog *d=new IQDialog();
n->handle=static_cast <void *>(static_cast < IObject *> (d));

Right, I have to cast it twice.

And here is how you get it back:

(static_cast < IObject * >(n->handle))->set_title ("Some title");

Is­n't this just ridicu­lous­ly weird for any­one com­ing from any oth­er lan­guage?

Or am I miss­ing some­thing com­plete­ly?

But the good news is, it works ok, and the Qt back­end now has a de­cent struc­ture to hack on.

IUP/Qt preview

Ok, here is ver­sion 0.0.1 of my Qt4 back­end for IUP . It's not very good, but a bunch of things work. A whole bunch more do not, of course.

I am show­ing this just be­cause I want to re­write it, be­cause I no­ticed that I am writ­ing it like it was Mo­tif stil­l, and I must un­der­stand this is ac­tu­al­ly C++ and Qt code ;-)

Us­ing it along with DI­UP you can write sil­ly Lin­ux/Win­dows/­Mac GUIs from D, or you can use C. No LUA yet.

You can down­load it here (click on the "Free" but­ton and wait a while).

You will need Qt4 and qmake to make it work.

IUP/Qt is advancing

I have call­backs (some), ge­om­e­try man­age­men­t, LED files load­ing (re­source files) and it seems no hard parts are left.

That would mean the re­main­ing work is routi­nary, even if it's a large amount of it.

It's nice that I can switch back and forth be­tween the Mo­tif and Qt im­ple­men­ta­tions just by set­ting LD_LI­BRARY_­PATH to com­pare :-)

IUP/Qt shows a window

Of course you know that means noth­ing, but yes, af­ter a few hours of hack­ing, I got enough of a Qt back­end for IUP so a win­dow can be dis­played.


Of course those are all the wid­gets that are im­ple­ment­ed cur­rent­ly.

If by im­ple­ment­ed you ac­cept "can't con­nect call­backs and 90% of the at­tributes are in­ac­ces­si­ble", that is!

But af­ter I get the call­backs work­ing (that's the hairy part), the rest is sim­ple mo­not­o­nous repet­i­tive work.

It's in­ter­est­ing that the Qt back­end is, of course, look­ing quite sim­pler than the Mo­tif one, and in fac­t, a fair bit sim­pler than the IUP pro­grams look.

But hey, you get a sort of LUA bind­ings for Qt at the same price...

OTO­H, the third step is a nicer OOP, D, wrap­per around IUP.

Or some­thing like that ;-)

Contents © 2000-2022 Roberto Alsina