[Mono-list] Re: Qt C# bindings and a simple HelloWorld

Richard Dale Richard_Dale@tipitina.demon.co.uk
Tue, 5 Feb 2002 10:25:08 +0000

On Tuesday 05 February 2002 2:04 am, Adam Treat wrote:
> So I've been mucking about for a bit with some c-sharp bindings to the
> qt-toolkit.  I've been successful with a generic helloworld program.  I
> think I am ready to begin generating some bindings in earnest after a few
> problems are solved:
> 1.  Not sure how to handle Slots and Signals.  (biggest problem) ;)
You need some kind of reflection/runtime metadata to do this - it has to be 
possible to query whether an instance implements a method/slot, and if so to 
call it. Then you need two C++ proxy classes to handle emitting signals and 
calling slots - eg CSSignal and CSSlot. 

> 2.  I have been using the qtc bindings from kde3 in the pinvoke statements.
> I would like to stop using the libqtc library and just dllimport from libqt
> but the name mangling (see
> http://www.delorie.com/gnu/docs/gcc/gxxint_15.html) is going to have to be
> accounted for.  Thinking about writing a tool that will automatically
> mangle the dllimport statement.  ie:
> 	From:
> 	[DllImport("libqtc.so.1.0.0")]
> 	public static extern Object qt_new_QApplication(int argc, string[] argv);
> 	To:
> 	[DllImport("libqt.so.2")]
> 	public static extern Object __12QApplicationRiPPc(int argc, string[]
> argv);
This would be a similar problem to deriving the JNI function names for java, 
in the kalyptus tool. See the perl function cplusplusToJNISignature() in 

> 3.  I don't know how to use kalyptus (don't know perl) and I am a frankly
> little frightened to go in that direction...  Thinking about using the kdoc
> html output and generating the bindings from that, similar to what the
> intel guys did with the ECMA documentation to c# stubs...  Richard, Miguel,
> Mike do you have some suggestions on the best way to proceed?
kalyptus is derived from kdoc - I started by hacking the html output exactly 
as you describe. It uses the same parser down to the method level, but adds 
argument level parsing and a 'typemap' to map Qt C++ data types onto C ones 
(and then from C types onto any other language type system).

> Well tell me what you think.
C# is so similar to Java that it shouldn't be a problem - I had to look twice 
at the example below to spot the difference! I would copy 
kalyptusCxxToJava.pm to kalyptusCxxToCS.pm, add a '-fcs' option to kalyptus 
and start hacking..

> P.S.  I am primarly a java programmer, with little experience in qt, c++,
> or c.  This is an exercise to hopefully do something useful and allow me to
> learn a little so feel free to offer up some pointers ;)
If you start by adapting the QtJava bindings, rather than starting from 
scratch it shouldn't be so hard. Mostly you have to write a lot of conversion 
functions to convert between C# string, collections, dates etc and the C++ 
equivalents. Have a look at these source files in QtJava - pretty much 
everything else is 'autogenerated' - it will give you an idea of how much 
work is involved.

        C++ proxies for Java signals and slots

        C++ static methods for running the library in conjunction with 

        A Java class with static methods for running the library, and to keep 
track of C++ -> Java instance mapping.

        Some native code methods to help the 'run the library' Java class 

        Utility class containing methods to invoke Qt event handlers and slots

-- Richard

> // qt_HelloWorld.cs - qt-csharp Hello World
> namespace QtSamples {
> 	using Qt;
> 	using System;
> 	public class qt_HelloWorld {
> 		public static int Main (string[] args)
> 		{
> 			QApplication a = new QApplication(args);
> 			QPushButton hello = new QPushButton("Hello world!", 0);
> 			hello.resize(100, 30);
> 			a.setMainWidget(hello);
> 			hello.show();
> 			return a.exec();
> 		}
> 	}
> }